I’m freshly new to C#, and need a bit of help. Trying to get my character sprite to face the direction of it’s movement (at least up, right, left, and down), but can only get it to rotate left and right. The up/down portions only make it turn to the left. Any help is appreciated.
public class puppyControllerScript : MonoBehaviour
{
public float maxSpeed = 10f;
Quaternion theRotation;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
theRotation = transform.localRotation;
}
void FixedUpdate ()
{
//Movement
float move = Input.GetAxis("Horizontal");
float vmove = Input.GetAxis("Vertical");
anim.SetFloat("Speed", Mathf.Abs(move+vmove));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, vmove * maxSpeed);
if (vmove > 0)
Up();
else if (vmove < 0)
Down();
else if (move > 0)
Right();
else if (move < 0)
Left();
}
void Up()
{
theRotation.z = 90;
transform.localRotation = theRotation;
}
void Down()
{
theRotation.z = 270;
transform.localRotation = theRotation;
}
void Right()
{
theRotation.z = 0;
transform.localRotation = theRotation;
}
void Left()
{
theRotation.z = 180;
transform.localRotation = theRotation;
}
}