- Home /
Question by
dxgriego · Mar 27, 2020 at 10:26 PM ·
rotationreflection
Bullet keeps rotating after ricochet off of wall
I tried to make a script for my bullet that calculates a ricochet direction and rotates the bullet in that direction when it hits a wall with the tag "Ricochet" but when it hits a wall it seems to continue rotating even after it leaves the wall. It continues traveling in a circular pattern and I can't figure out how to make it travel in the straight line like it is supposed to. Here's my code:
private void FixedUpdate()
{
transform.position += transform.forward * Time.deltaTime * speed;
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "Ricochet")
{
Vector3 reflectDir = Vector3.Reflect(transform.forward, collision.GetContact(0).normal);
Quaternion rotation = Quaternion.LookRotation(reflectDir);
transform.eulerAngles = new Vector3(0, rotation.eulerAngles.y, 0);
}
}
Comment
The problem ended up being with the bullet colliding with the wall and the physics system attempting to "help" by calculating alternate velocities. I ended up just freezing all rotation and position axis.