The question is answered, right answer was accepted
GameObject not looking at me..
Hi ! I got a problem. Surfed the web a lot to find a solution, But I don't know, i tried so much possibilities and variants, and it still doesn't work :( ! I'm trying to make a gameObject ( a creature ) rotating to look at me and then moving towards me. Here's my code, I really don't understand why the creature doesn't turn to me. Sorry for bag english grammar, I'm french c':
using UnityEngine;
using System.Collections;
public class IA : MonoBehaviour {
//Déclaration des variables
bool joueurVu = false;
public GameObject joueur;
float mouvementSpeed = 2.5f;
// Use this for initialization
void Start () {
joueur = GameObject.Find ("FPSController");
}
// Update is called once per frame
void Update () {
if (getDistance () < 10) {
joueurVu = true;
print ("Player seen!");
}
if (joueurVu) {
transform.position = Vector3.MoveTowards (transform.position, joueur.transform.position, Time.deltaTime * mouvementSpeed);
transform.LookAt (joueur.transform.position);
}
}
//Autres méthodes
float getDistance(){
return Vector3.Distance (joueur.transform.position, transform.position);
}
}
Answer by ElijahShadbolt · Nov 15, 2016 at 07:05 AM
That is a mystery. It works fine for me.
Maybe you put the script on the wrong object? It should be on the highest parent in the hierarchy of the creature.
I'm not sure about animations, but could they possibly be set to world space, to not update with rotation?
Also, on the 'if' statement on line 19, you might want to add an 'else' statement to set joueurVu to false.
if (getDistance () < 10) {
joueurVu = true;
// ...
} else {
joueurVu = false;
}
I will check that ! Jup adding an "else" sounds good ahah :P