- Home /
2D Game - Y axis rotation (left - right)
Hello everyone, i'm doing a simple 2D Game. Of course, trough < and > arrows you're allowed to walk. I made idle and walk animations successfully, now i would like to rotate my sprite to left when he's walking to left, and to right when he walks to right.
I tried many rotation functions but i made some mistakes, can i know the right way? P.S: I'm using C#.
Here's my Player code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed = 4;
public float position;
protected Animator animator;
void Start () {
animator = GetComponent<Animator>();
position = transform.position.x;
}
void Update () {
position = transform.position.x;
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * Speed * Time.deltaTime);
if(position < transform.position.x) {
animator.SetBool("WalkVar", true);
} else if(position > transform.position.x) {
animator.SetBool("WalkVar", true);
} else {
animator.SetBool("WalkVar", false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Multi Touch GUI buttons 1 Answer
2D sprite that always faces the player? 1 Answer
2D sprite rotation with velocity and interpolation 1 Answer
2D : Rotation of Object With x-y Axis 2 Answers