- Home /
Play incorrect animations when is looking in other direction
i am learning unity and i am making a top down game, the player look where the mouse is, when i look up the player do the correct animations, but when i look in other direction the player do the animations like he was looking up, i am trying to make him to play the animations relative of where he is looking.
Here is PlayerController
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
Rigidbody rigidBody;
Animator anim;
Ray ray;
RaycastHit hit;
Vector3 movement;
float horizontal;
float vertical;
void Awake() {
rigidBody = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
void FixedUpdate(){
Move();
Turning();
PlayAnimations();
}
void Move() {
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
movement = new Vector3(horizontal, 0f, vertical).normalized;
rigidBody.MovePosition(transform.position + movement * Time.deltaTime * speed);
}
void Turning() {
if (Input.GetMouseButton(1))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f))
{
Vector3 playerToMouse = hit.point - transform.position;
playerToMouse.y = 0;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
rigidBody.MoveRotation(newRotation);
}
}
}
void PlayAnimations() {
anim.SetFloat("Horizontal", horizontal);
anim.SetFloat("Vertical", vertical);
}
}
Comment