- Home /
The question is answered, right answer was accepted
Multiple Raycasts & Vector3.Reflects in a row. How?
I'm trying to make a raycast reflect off multiple surfaces. I can successfully get one reflection but am currently stuck on how to do multiple.
I guess I'm stuck on where to start the second raycast from. Can anyone help me figure this out?
RaycastHit hit2;
RaycastHit hit3;
Debug.DrawRay(transform.position, transform.up * 100f, Color.green);
//First raycast and reflect
if(Physics.Raycast(transform.position, transform.up, out hit2, 100f))
{
Vector3 newDir = Vector3.Reflect(transform.up, hit2.normal);
Debug.DrawRay(hit2.point, newDir * 100f, Color.green);
//Attempting next raycast and reflect.
if(Physics.Raycast(hit2.point, newDir, out hit3, 100f ))
{
Vector3 newDir1 = Vector3.Reflect(transform.up, hit3.normal);
Debug.DrawRay(hit3.point, newDir1 * 100f, Color.green);
}
}
Answer by highpockets · May 26, 2019 at 03:04 PM
The second raycast direction is the Vector3 that the Reflect() function returns. Just keep doing the exact same all the way along. You give Reflect an inNormal and inDirection and it gives you the direction you need. Shoot a ray in that direction. The direction returned by the function is the equal magnitude/length of the inDirection, so if you would prefer normalizing it for readablility/usability, just use Vector3.Reflect(inDirection, inNormal).normalized;
I guess I'm just not understanding properly. I can get another ray to fire, but not from where I want.
Right now I'm trying to draw a ray after the second reflection.
if (Physics.Raycast(newDir, transform.up, out hit2, 100f ))//third ray
{
Vector3 newDir1 = Vector3.Reflect(transform.up, hit2.normal);
Debug.DrawRay(hit2.point, newDir * 100f, Color.red);
}
But this is causing a ray to fire way up at the top of the screen. What should I be setting for the new raycast? Currently I'm using newDir, which is the reflected vector3, but I assume that's not correct.
Your passing mixed messages to the Raycast function. The first parameter that you are passing should be the origin (start position) of the Ray. The next parameter is the direction. You are passing the newDir first and then transform.up (transform.up is just the normalized y axis direction of the transform that this script is on), but I don’t think you need that even in the first raycast. Say you wanted to shoot your raycast out firstly from your player gun along his forward z axis. $$anonymous$$aybe, you would pass the first parameter gun.transform.position and then you would pass the second parameter gun.transform.forward. Then do your reflection and the you pass the hit.point as the first parameter and then the reflectDirection as the second parameter, but you can just make this raycast/reflect logic a single method
Follow this Question
Related Questions
Problem With Raycasthit Angle 0 Answers
Spawn script on mesh crashes Unity 0 Answers
How to move the main camera in Google Cardboard? 0 Answers
UI Text showing weird string value? 1 Answer
How to access scripts in other game object with raycasts? 2 Answers