How to make player rotate 180° and run that direction with mecanim + vice versa?
My player has the X-axis locked, so the player can only jump and move forward/backward. I'm trying to make it so the player runs forward (with W) and then stops and runs backward when the "S" key is pressed and vice-versa.
I have all the animations to do this.
My player script:
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float rotationSpeed = 5.0f;
private float playerDirection;
private float playerSpeed;
private Vector3 moveDirection = Vector3.zero;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
if (Input.GetKeyDown(KeyCode.S))
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.W))
{
transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
}
playerDirection = Input.GetAxis("Horizontal");
playerSpeed = Input.GetAxis("Vertical");
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
anim.SetFloat("Direction", playerDirection);
anim.SetFloat("Speed", playerSpeed);
}
}
Comment
Best Answer
Answer by Wach3D · Feb 10, 2017 at 06:50 PM
Found the answer!
http://answers.unity3d.com/questions/21628/character-turning-around.html
Your answer
Follow this Question
Related Questions
Animate Physics, Character movement problem 0 Answers
Humanoid Animations 0 Answers
Mecanim Generic No movement 0 Answers
Want to create MediaPlayer like thing but for animator. 0 Answers
How do I move two first person shooter hands at once using animation? 0 Answers