how to ignore transform.position.y

In here I am trying to get the gameobject (dog) to come to its owners front (just like you would call a dog). I am trying to get it to ignore the players y position though. With this, the error is that the loop never breaks. When the function comeForward is called, it’ll go the leaders front, but will trip out and never be still; it rapidly moves back and forth at the position.

If I just use transform.position instead of current position, it’ll work, but will trip out just the same if on uneven terrain, hence my attempt to get it to ignore the y position of the leader.

function Update(){

var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
moveDirection.y -= gravity * Time.deltaTime;

//etc


}if (comeHere){
animation.CrossFade("walk");
controller.SimpleMove(forward * speed);
}

}




function ComeForward(){


var currentPosition = transform.position;
    currentPosition.y = leader.position.y;
    while ( Vector3.Distance(  leader.position + leader.forward*3 , currentPosition ) >= .5) { 

        var lookPos = leader.position  - transform.position;
        lookPos.y = 0;
        var rotation = Quaternion.LookRotation(lookPos);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed); 

        comeHere = true;
        yield;
    }

    transform.LookAt(leader.position + leader.right * 5);
    animation.CrossFade("idle");
    comeHere = false;

   } 

What am I missing?

Your while loop is using a different goal position than your lookPos. So the gameobject moves towards leader.position until it gets to leader.position+leader.forward*3, which of course won’t happen.

var point2 : Vector3 = leader.position;
point2.y = transform.position.y;

		while (Vector3.Distance(point2 + leader.forward*3 ,  transform.position) > .5){
		
		transform.LookAt(point2 + leader.forward*3);
		
		comeHere = true;
	
		yield;
		
		}
		transform.LookAt(leader.position + leader.right * 5);
		animation.CrossFade("idle");
		comeHere = false;
		}

i was having a similar issue. i did not read all the posts here, but i ended up solving my issue by making my starting “y” value and my ending “y” value the tranform’s “y” value.

transform.position = Vector3.MoveTowards(Vector3(transform.position.x, transform.position.y, transform.position.z), Vector3(targetPoint.x, transform.position.y, targetPoint.z), speed);

transform.position = Vector3.MoveTowards(Vector3(transform.position.x, transform.position.y, transform.position.z), Vector3(targetPoint.x, transform.position.y, targetPoint.z), speed);

i separated the first vector3 to show/compare, but it’s not needed, same thing could be done like this:

transform.position = Vector3.MoveTowards(transform.position, Vector3(targetPoint.x, transform.position.y, targetPoint.z), speed);

i really wish you could just tell it to “ignore” a value, i’ve had mixed results with vector2, for some reason, though it seems it should be the same thing… well, i’m still noob level so maybe i’ll figure it out someday.