Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Raycasts and hits

Raycasts and hits

3,915 0

I was planning on creating some cases with inheritance for items and weapons and stuff but i just don't have the time so I'm going to make it simple and have only one gun option and no consumable items. so i need a simple script that will take input from the space key and use the position and rotation of the gun to determine if it hits something then if that object is on the 8th layer which is the enemy layer it will then register that as a hit, i used World of Zero's tutorial for this. I'll then need to sort out some sort of health system but for now its just destroying whatever it hits. So, i have the raycast working and will move on to the health system and how it will send that hit to the enemy itself.

calculating the ray cast from the gun and checking the hit object

after some quick googling i used a video by gameplusjames to figure out how to call the functions from the other game objects. Now i have the enemy taking damage from the player when they are shot and i have the player taking damage from the enemy when they're within range of being hit. The only issue now is that there is no cooldown on any of the attacks so it happens rather quickly. i'll have to figure out the best way to implement the cooldown.

calling Function from different script on another game object

Ok, I found a simple piece of code on the unity forums that just sets the time of the next possible attack by adding the time of the previous attack to the attack delay so i implemented that and i now have an attack delay. So, it feels like i have the core game loop running now. the 2 main things i need to implement now are the spawn manager to spawn in the enemies and a whole UI system and an actual map to play on. The map sounds like the easiest and quickest of those so im just going to start with that.

Attack Delay



World of Zero. Detecting Objects with Raycasts in Unity3D [Video]. YouTube. https://www.youtube.com/watch?v=vqc9f7HU-Vc​

gamesplusjames. Unity RPG Tutorial #17 - Enemy Health/Damage System [Video]. YouTube. https://www.youtube.com/watch?v=6RtMDm7sGKs​

ray_sovranti.6120724. (2020, November 03). What is the best way to delay my Attack() method [Online Forum Post]. Unity. https://forum.unity.com/threads/what-is-the-best-way-to-delay-my-attack-method.999343/​


Discussion

Loading comments...