Walking on Air

I’m using the following script to have a character walk towards an object.

var target : Transform;

var player : GameObject; 
var speed : float = 1; 

var soldierSmith : GameObject;

soldierSmith.animation["walk"].wrapMode = WrapMode.Loop;
soldierSmith.animation["idle"].wrapMode = WrapMode.Loop;

function Start () { 
     soldierSmith.animation.CrossFade("idle");
} 

function Update() { 
	//soldierSmith.animation.CrossFade("walk");

     var distance = Vector3.Distance( player.transform.position, transform.position); 

     if ( distance < 100  ) 
     { 
          var delta = player.transform.position - transform.position; 
          delta.Normalize(); 
          
          var moveSpeed = speed * Time.deltaTime; 
      
          transform.position = transform.position + (delta * moveSpeed); 
    } 
}

However, as he walks, the further he walks, the higher he gets in Y. He keeps getting higher and higher. Any ideas why that would be happening?

I replaced:

transform.position = transform.position + (delta * moveSpeed);

with

transform.Translate(speed*Vector3.forward*Time.deltaTime);

And it worked. Why? I have not idea… :sweat_smile:

is it possible that he is moving towards the center or pivot of the object causing him to move upwards as he gets closer to it?

if so i would recomend freezing the Y position of the character, unless of course he is moving across uneven terain then i’m not sure.

Well, transform.position is a Vector3. You are adding a Vector3 to a non-vector type. I’m surprised Unity allows one to do this, but I’m assuming it is doing some weird conversion, which led to the character increasing in the Y-axis.

Anyways, it’s a good idea to be using character controller if it’s for a character. Take a look at the Unity docs.