Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bones rotation from script with LateUpdate

Discussion in 'Scripting' started by bogdan222, Jul 9, 2017.

  1. bogdan222

    bogdan222

    Joined:
    Mar 6, 2017
    Posts:
    33
    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:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AI_chasing : MonoBehaviour {
    7.     static public Transform player;
    8.     [SerializeField]private float speed;
    9.     [SerializeField]private float movespeed;
    10.     [SerializeField]private Transform head;
    11.     private  Animator anim;
    12.     // Use this for initialization
    13.     void Start () {
    14.         player = GameObject.Find ("FPSController").transform;
    15.         anim=this.GetComponent<Animator>();
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         Vector3 direction = player.position - this.transform.position;
    21.         direction.y = 0;
    22.         //    float angle = Vector3.Angle (direction, head.forward);
    23.         if (Vector3.Distance (player.position, this.transform.position) < 10) {
    24.             this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), speed * Time.deltaTime);
    25.             anim.SetBool ("is_idle", false);
    26.             if (direction.magnitude > 1.3f) {
    27.                 this.transform.Translate (0, 0, movespeed * Time.deltaTime);
    28.                 anim.SetBool ("is_running", true);
    29.                 anim.SetBool ("is_attacking", false);
    30.             } else {
    31.                 anim.SetBool ("is_attacking", true);
    32.                 anim.SetBool ("is_running", false);
    33.             }
    34.         } else {
    35.             anim.SetBool ("is_idle", true);
    36.             anim.SetBool ("is_running", false);
    37.             anim.SetBool ("is_attacking", false);
    38.         }
    39.     }
    40.     void LateUpdate()
    41.     {
    42.         Vector3 direction2 = player.position - head.transform.position;
    43.         head.transform.rotation = Quaternion.Slerp (head.transform.rotation, Quaternion.LookRotation (direction2), speed * Time.deltaTime);
    44.     }
    45. }
    46.  
    I don't know why, please help. :L
     
  2. Roy-Massaad1

    Roy-Massaad1

    Joined:
    Jun 30, 2015
    Posts:
    16
    any updates with it ?
     
  3. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    It looks like the variable "head" is not being assigned a value...
    If it would be filled with the right Transform, you need to use head.rotation instead of head.transform.rotation. (same with position)
    Hope it helps.