- Home /
Question by
anetteagura · Jul 21, 2020 at 02:51 PM ·
2d game2d rotation
How can my sprite move to new direction it's facing not keep going to old direction?
Hello everyone! I'm quite new to Unity. Only just starting and trying to figure things out. I have a code, but it will still move to the direction it was facing even when I rotate the sprite. I hope someone can help :)
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
GetPlayerInput();
}
private void FixedUpdate()
{
MovePlayer();
}
private void GetPlayerInput()
{
_hInput = Input.GetAxisRaw("Horizontal");
_vInput = Input.GetAxisRaw("Vertical");
}
private void MovePlayer()
{
float rotation = -_hInput * rotSpeed;
transform.Rotate(Vector3.forward * rotation);
if (Input.GetKey("w"))
{
rb.velocity = new Vector2(_hInput,_vInput);
}
if (Input.GetKey("s"))
{
speed -= increase * Time.deltaTime;
speed = Mathf.Clamp(speed, 0f, maxSpeed);
}
if (speed == 0)
{
rb.velocity = Vector3.zero;
}
}
}`
Comment