- Home /
How to turn player face when moving left and right direction ?
**// trying to use this** transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation (transform.position - prevLoc), Time.fixedDeltaTime * 2f);
anim.SetBool ("Player_move", false);
// Debug.DrawLine (transform.position, GroundObject.position, Color.red);
Grounded = Physics.Linecast (this.transform.position, GroundObject.position, 1 << LayerMask.NameToLayer ("Ground"));
Debug.Log ("Grounded" + Grounded);
if (Input.GetKey (KeyCode.LeftArrow)) {
CubeTransform.Translate (new Vector3 (0, 0, -5) * Time.deltaTime);
anim.SetBool ("Player_move", true);
// CubeRigidBody.velocity=new Vector3(-2, 0, 0);
}
if (Input.GetKey (KeyCode.RightArrow)) {
CubeTransform.Translate (new Vector3 (0, 0, 5) * Time.deltaTime);
anim.SetBool ("Player_move", true);
// CubeRigidBody.velocity = new Vector3(2, 0, 0);
}
if (Input.GetKey (KeyCode.Space) && Grounded) {
CubeRigidBody.AddForce (new Vector3 (0, 1.8f, 0), ForceMode.Impulse);
}
}
Answer by the_genius · May 02, 2018 at 05:27 PM
There are a few methods:
Method 1:
Change the transform.rotation value.
When the right arrow key is down do the following:
transform.localRotation = Quaternion.Euler(0, 0, 0);
When and when the left arrow key is down:
transform.localRotation = Quaternion.Euler(0, 180, 0);
Method 2:
Just flip the local x scale of the sprite: When the right arrow key is down do the following:
transform.localxscale = 1;
When and when the left arrow key is down:
transform.localxscale = -1;
Method 3:
Get a reference to the SpriteRenderer component and use SpriteRenderer.flipX. I don't think this method would flip a 2d collider so you might have to that seperatley.
Quaternion.lerp is meant to be used to smoothly change the rotation from one direction to another. If you want it to change direction instantly the methods I mentioned are much better as you won't get a delay and performance would be better.