- Home /
Wrong contineous rotation to car
In my 2d game, I want to rotate car face based on collision detected. But at present after 3 to 4 collision its doing continuous rotation only rather than moving straight after collision.
Here is my straight movement related code:
myRigidbody.MovePosition (transform.position + transform.up * Time.deltaTime * GameManager.Instance.VehicleFreeMovingSpeed);
For better collision purpose, I have used Rigidbody position change approach.
Here is my collision time code:
public void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.CompareTag (GameConstants.TAG_BOUNDARY)) {
ContactPoint2D contact;
contact = other.contacts [0];
Vector3 reflectedVelocity = Vector3.Reflect (transform.up, contact.normal);
direction = reflectedVelocity;
}
}
if (direction != Vector3.zero && pathGenerator.positionList.Count <= 0) {
float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion targetRot = Quaternion.AngleAxis (angle - 90f, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRot, 0.1f);
if (Quaternion.Angle (transform.rotation, targetRot) < 5f) {
transform.rotation = targetRot;
direction = Vector3.zero;
}
}
Rotation get applied within other code so that car was changing its face correctly and that method not affecting here that I have checked by placing debug statement.
Share you side thought for this.
Your answer
Follow this Question
Related Questions
How do i determin if an objects rotation is between 2 values on each axis? 0 Answers
How to lock rotation in z of my camera while rotating the camera by using mobile gyroscope input 0 Answers
I'm trying to create a game that you move forward and back and rotate left and right. 0 Answers
Low memory crash on IOS when changing orientation 1 Answer
how do i make first person character rotate left and right along with camera? 0 Answers