- Home /
Turn 180 animation
Hello, I have a camera that determines the vector. There is a character who, when I press W, turns in the direction of the camera, and if I press S, then he turns to face the camera.
Movement script:
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
animator.SetFloat("Horizontal", Horizontal);
animator.SetFloat("Vertical", Vertical);
Vector3 move = Quaternion.Euler (0, playerCamera.transform.eulerAngles.y, 0) * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (isAttack == false)
{
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
animator.SetBool("Walk", true);
}
else
{
animator.SetBool("Walk", false);
}
Next, I calculate the player's rotation angle to write the rotation to the animator:
var targetDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetDirection.y = 0;
Quaternion targetRotation = Quaternion.identity;
Vector3 playerRelativeTargetDir = transform.InverseTransformDirection(targetDirection);
if (targetDirection != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
}
animator.SetFloat("RelativeEulerDir", targetRotation.eulerAngles.y);
Problem: I get a static angle, going forward 0, going backward 180 and so on.
Question: How can I get an angle of 180 with the opposite movement towards the camera, and then reset this value so that in the future, when the opposite movement is repeated, this angle is 0, and when moving forward relative to the camera, I get 180 again, respectively, when moving forward again away from the camera, this angle became 0 again.
I roughly understand that you need to take the player's coordinates so that it would be the direction forward, but I always get 0, since the rearrangement turns instantly, not having time to give direction.