- Home /
Reflect bullets in 3D world space
I want bullet to bounce/reflect mirroring the position they hit. I have it working... kind of. All collision faces facing a particular direction bounce the bullets off close enough to look correct. But any other facing direction of the colliders bounces incorrectly.
if (reflect)
{
RaycastHit hit;
if (Physics.Raycast(this.transform.position, Vector3.forward, out hit, 10f))
{
Vector3 incomingV = hit.normal - transform.position;
Vector3 reflectV = Vector3.Reflect(incomingV, hit.normal);
//Debug.DrawRay(hit.point, reflectV, Color.green, 10f);
Transform hitBounce = PoolManager.Pools["AMMO"].Spawn("BoltReflected");
hitBounce.position = hit.point;
hitBounce.eulerAngles = reflectV;
}
}
I have tried a few variations but it just doesn't seem to ever work cleanly. Could someone please point out to me what I've gotten wrong?
Answer by HarshadK · Apr 11, 2016 at 12:39 PM
It is working for a specific direction only because you are using Vector3.forward in your raycast hit, which will always be the same direction i.e. (0, 0, 1). What you want is the forward direction of your bullet, for which you need to use Transform.forward.
Just change your line of RaycastHit to use forward of your bullet's transform as below:
if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, 10f))
You beat me to the answer :p
But to expand on the answer:
Vector3.Reflect wants a direction and a normal. Currently only the normal is provided, the other is a bit odd.
The inco$$anonymous$$g direction should be something like the forward of your bullet. (Same as the direction of the raycast).
I'd expect this should work better (assu$$anonymous$$g that this script is added to the bullet, and the bullet is moving in the same direction as his own forward)
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 10f))
{
Vector3 reflectV = Vector3.Reflect(transform.forward, hit.normal);
//Debug.DrawRay(hit.point, reflectV, Color.green, 10f);
}
Also, the returned value of Vector3.Reflect are not eulerangles. They represent a forward. So setting your eulerangles to a forward should cause some odd behaviour. I'd advise to either set the forward or set the rotation to Quaternion.SetLookRotation.
Thanks! Looks like I've used a lot of things incorrectly here. I haven't used SetLookRotation before. Would this be the correct way to use it: hitBounce.rotation.SetLookRotation(reflectV, Vector3.up); The bullets are bouncing offset from the hit.point currently.
Alrighty, sorted! Thanks again @Harshad$$anonymous$$ and @troien. Turns out I was best to delete all I had and rewrite it.
if (reflect)
{
RaycastHit hit;
if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, 10f))
{
Vector3 reflectV = Vector3.Reflect(transform.forward, hit.normal);
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation(reflectV);
}
}
The spawning of new instances for the bounce was causing some major delay and so the reflect looked like it was offset quite a bit. So removed the spawn and prevented the bullet from despawning on hit.
Thanks again! Was stuck on this for hours.
I just needed to make a $$anonymous$$or tweak to prevent the bullets from passing through the collider before bounce.
if (Physics.Raycast(this.transform.position - this.transform.forward, this.transform.forward, out hit, 10f))
Your answer
Follow this Question
Related Questions
Ricochet Trajectories C# 1 Answer
Multiple Cars not working 1 Answer
Set Velocity at relative position. 2 Answers
What is the error? 2 Answers
Punching objects (C#) 1 Answer