- Home /
Question by
liadovdat5 · Jun 17, 2020 at 05:36 PM ·
rotationdirectionplayer movement
Player stops move the character direction resets
Hello everyone ! My character is a car and I try to rotate it the direction it move, so far so good I succeeded to do that but once I stop moving the character flips back to the direction it was on the start. Also how can I make my turns from side to the opposite site smooth ? Here is my code so far:
[SerializeField] float driveSpeed = 5f;
//state
Rigidbody2D myRigidbody;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
//Control of velocity of the car
float HorizontalcontrolThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // Value between -1 to 1
float VerticalcontrolThrow = CrossPlatformInputManager.GetAxis("Vertical"); // Value between -1 to 1
Vector2 playerVelocity = new Vector2(HorizontalcontrolThrow * driveSpeed, VerticalcontrolThrow * driveSpeed);
myRigidbody.velocity =playerVelocity;
//Direction of the car
Vector2 direction = new Vector2(HorizontalcontrolThrow, VerticalcontrolThrow);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
myRigidbody.rotation = angle;
}
}
Comment
Your answer
Follow this Question
Related Questions
Player rotating towards direction of movement in 3D 1 Answer
Rotate player in movement direction 1 Answer
Character rotating to previous rotation 1 Answer
Getting the face direction 2 Answers