How to make my sprite rotate to face the direction they are currently moving? (2D)
Hi. I'm making a top-down game and have got the movement for my sprite down. I've made it so whenever I right-click a location, my unit goes towards that location. The problem is that they do not rotate while moving, so they are always facing right, even if moving left, up, down, etc. I'd like to make it so they rotate to face the direction they are moving. For example, face left when moving left, then move down when going down, and look north when going that direction. I've been trying for days and have not been able to figure it out/find a proper tutorial to help me. Any help at all would be appreciated. My code is below.
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour {
public float speed = 1.5f;
private Vector3 target;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
Vector2 moveDirection = gameObject.GetComponent<Rigidbody2D>().velocity;
if (moveDirection != Vector2.zero)
}
}
// I'm very new and inexperienced, so I appreciate anyone helping. Thank you.