- Home /
How to scatter Ray Casts? Video Example Included.
How can I scatter raycasts and not have it directly forward of the player? I also would like it to not be instantly shot. It should take time to get to the target. Look at this video and you will understand what I want. It starts at 1:37. Here: http://www.youtube.com/watch?v=nVvqJ1KKM04
Thanks guys!
Answer by llSalvationll · May 28, 2013 at 10:15 PM
You asked 2 questions: 1-Scattering Bullets- For this piece, it sounds like you probably already have a vector pointing directly forward of the player. From this vector you can use the Random class to add/subtract(or multiply) components(x,y,z) of the vector to alter the course 'randomly'. You will need to experiment until you find value ranges that get you the scatter behavior you want.
2-Delay in reaching target- If you want a delay in reaching the target, you probably don't want to use a raycast at all, but rather instantiate a projectile - i.e. create a gameObject of some kind. Give the object you instantiate the normalized vector you calculated in the scatter logic, and have it translate toward that point at whatever 'speed' you like and multiply that by delta time. Psuedo-code looks something like this: bulletTransform.Translate(normalVector bulletSpeed Time.deltaTime). You'll likely have to experiment with the speed value here as well until you get something you're happy with. Hope this helps you.
But I'm the video the ray cast took time to get to the target I don't want to instantiate bullets I want to use ray casts. Thanks for the nice reply
The video shows rendered lines. The built-in raycast for Unity does not have a visual component or speed to it. You can use the logic above to get your random vectors and then multiply those vectors over several frames to increase their length. It looks like in the video they're using debug.drawline to show the size/location of the raycasts they're doing - you'll notice the line just gets longer and longer until it hits something (or goes off the screen).
Answer by hd27 · May 28, 2013 at 10:19 PM
You can try taking the forward vector (the direction your shooting) and then cast the rays in a random range around that vector.
Your answer
Follow this Question
Related Questions
Best way to shoot physical bullets? 2 Answers
Particular collider not working properly... 1 Answer
Multiple raycasts in same fixedupdate 3 Answers
Physics.SphereCastAll with triggers 1 Answer