- Home /
Bullet reflect not working properly
I'm trying to have a bullet projectile using raycast as collider to bounce off certain reflective objects. From what I can tell, it doesn't seem to mainly reflect based on where the bullet hits the object, but based on the reflective object's rotation. If the reflective object's rotation is 0, then no matter where the bullet hits it from, it will reverse its speed like it's hitting the object straight on.
I'm assuming the code I have does not take the rotation of both bullet and object into account. What can I change to make this work? Thanks in advance.
Code below here:
using UnityEngine; public class BulletBounce : MonoBehaviour { public float time; public float speed;
private Vector3 direction;
void Awake()
{
direction = Vector3.forward;
}
void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(direction), out hit, speed))
{
if (hit.transform.tag != "Player" && hit.transform.tag != "IgnoreBullet")
{
if (hit.transform.tag == "Reflect")
{
direction = Vector3.Reflect(direction, hit.normal);
transform.position = hit.point;
transform.rotation.SetFromToRotation(Vector3.forward, direction);
return;
}
}
}
transform.Translate(direction * speed);
}
}
Answer by Namey5 · Aug 26, 2019 at 03:35 AM
transform.Translate works in object space, but your direction is in world space. Replace the last line with this;
transform.position += direction * speed;
By doing that, it seems that the bullet's movement does not take it's own rotation into account, as it only initially moves forward on the world z-axis. Is there a potential way to have it both move on it's own z-axis while being able to reflect off of objects?
Sorry, didn't realise you also initialised it to a local space direction. In your awake function, replace the line with this;
direction = transform.forward;
It should start in the right direction now.
After changing the line "if (Physics.Raycast(transform.position, transform.TransformDirection(direction), out hit, speed))" to "if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, speed))", I think I've got it working for now.
$$anonymous$$any thanks for your help!