I am shooting projectiles and boucing them off the walls - I want sort of unreal perfect bounces - the object doesnt lose velocity and doesnt start spinning on collision - only is rotated once to "match" the reflect direction.
I use physics material for that has 0 friction and 1 bounciness on the walls and the projectiles, I use gravity scale on projectile 0 and its mass 0,0001 - the lowest possible amount and the projectiles rigid body has disabled rotation.
Everything is working fine but I can't get the projectile bounce rotation right, I rotate it's transform on collision this way:
public class Laser : MonoBehaviour {
private new Rigidbody2D rigidbody2D;
private Vector3 oldVelocity;
private void Start() {
rigidbody2D = GetComponent<Rigidbody2D>();
boxCollider2D = GetComponent<BoxCollider2D>();
}
void FixedUpdate () {
oldVelocity = rigidbody2D.velocity;
rigidbody2D.freezeRotation = true;
}
void OnCollisionEnter2D (Collision2D collision) {
ContactPoint2D contact = collision.contacts[0];
Vector3 reflectedVelocity = Vector3.Reflect(oldVelocity, contact.normal);
rigidbody2D.velocity = reflectedVelocity;
Quaternion rotation = Quaternion.FromToRotation(oldVelocity, reflectedVelocity);
transform.rotation = rotation * transform.rotation;
}
}
Currently looks like this - stepping frame after frame
I want it to rotate around the collision point like this on the right:
tried this without luck:
transform.RotateAround(contact.point, new Vector3(0f, 0f, 1f), Vector2.Angle(oldVelocity, reflectedVelocity));
RotateAround
? – derHugoVector2.Angle
returns an unsigned angle .. you might want to tryVector2.SignedAngle
instead. If it happens infact allways you should invert the angle ;) – derHugo