So i have an AI that have some animations but i also need the head to rotate to player direction but its not working neither in late update . Here is the Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI_chasing : MonoBehaviour {
static public Transform player;
[SerializeField]private float speed;
[SerializeField]private float movespeed;
[SerializeField]private Transform head;
private Animator anim;
// Use this for initialization
void Start () {
player = GameObject.Find ("FPSController").transform;
anim=this.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
// float angle = Vector3.Angle (direction, head.forward);
if (Vector3.Distance (player.position, this.transform.position) < 10) {
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), speed * Time.deltaTime);
anim.SetBool ("is_idle", false);
if (direction.magnitude > 1.3f) {
this.transform.Translate (0, 0, movespeed * Time.deltaTime);
anim.SetBool ("is_running", true);
anim.SetBool ("is_attacking", false);
} else {
anim.SetBool ("is_attacking", true);
anim.SetBool ("is_running", false);
}
} else {
anim.SetBool ("is_idle", true);
anim.SetBool ("is_running", false);
anim.SetBool ("is_attacking", false);
}
}
void LateUpdate()
{
Vector3 direction2 = player.position - head.transform.position;
head.transform.rotation = Quaternion.Slerp (head.transform.rotation, Quaternion.LookRotation (direction2), speed * Time.deltaTime);
}
}
I don’t know why, please help. :L