- Home /
Question by
$$anonymous$$ · Jan 09, 2014 at 07:38 PM ·
javascriptspeedmathmove
Move speed not working properly
i have a script wich moves a ai character towards the player. the problem is that even though i change the move speed variable it does not seem to matter. the enemy still moves really fast.
#pragma strict
var player : GameObject;
var moveSpeed : float = 0.000001;
var detectDistance : float = 10;
var minDistance : float = 1.5;
function Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
function Update () {
var distance = Vector3.Distance(gameObject.transform.position, player.transform.position);
if(distance >= minDistance && distance <= detectDistance){
Debug.DrawLine(player.transform.position, gameObject.transform.position, Color.red);
gameObject.transform.LookAt(player.transform);
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed, Space.Self);
}else{
Debug.DrawLine(player.transform.position, gameObject.transform.position, Color.green);
}
}
Comment
Best Answer
Answer by robertbu · Jan 09, 2014 at 07:41 PM
Variables by default are public in Javascript. This means that once a script is attached, you must change public variables in the Inspector. If you select the game object in the Hierarchy, then click on the gear icon in the upper right of your script in the Inspector, you can select 'Reset' to cause the script to reread the variables. Alternately you can just make your variables private:
private moveSpeed : float = 0.0000001;
OH. Wow. i have used unity for 1-2 years and i have not noticed that before now. :P