# How to disable diagonal Vector3 movement of non-player object using MoveTowards?

Hi everyone,

My 3D game has a NPC sprite moving towards it’s target object and stopping once it reaches it.
I’m after some help on how to prevent the sprite moving diagonally. I understand it must be something like using GetAxis and telling the object to not move on the x-axis if the y-axis has movement but I just can’t figure it out exactly. With my player, putting a “return;” after each of it’s movement inputs did the job perfectly but I believe that doesn’t really apply to this because I’m not specifically addressing each direction of Vector3 movement right?

Apologies for my very amateur coding knowledge. I’d hate to be one of those people on here who asks silly questions and tries to get people to do it for them. I’ve been stuck on this for a couple hours however and couldn’t solve it looking at all the other questions on this lovely site or others…

Here’s my code:
Thanks a lot

``````#pragma strict

var target: Transform;
var speed: float;
var thereyet: boolean;

function Start(){
thereyet = false;
}

function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag=="Position_1"){
thereyet = true;
}
}

function OnTriggerExit (other : Collider) {
if(other.gameObject.tag=="Position_1"){
thereyet = false;
}
}

function Update(){
if (thereyet == false){
var step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
``````

One simple solution:

(C#)

``````void Update () {

float step = speed*Time.deltaTime;

//vector math 101
//target.position - my.position will give you direction to target position from my position:
Vector3 directionToTarget = target.position-transform.position;

//Math.Abs() is merely to make every value positive so we can compare lengths regardless of their -+ sings
//simply put i'm checking here is directionToTarget.x shorter or directionToTarget.y (and not near zero(Mathf.Epsilon is a shorthand for near-zero value))
//and then setting to zero other direction
if( Mathf.Abs( directionToTarget.x )<Mathf.Abs( directionToTarget.y ) && Mathf.Abs( directionToTarget.x )>Mathf.Epsilon ) {
directionToTarget.y = 0f;
}
else if( Mathf.Abs( directionToTarget.y )>Mathf.Epsilon ) {
directionToTarget.x = 0f;
}

//my.position + directionToTarget.position gives targetPosition again:
transform.position = Vector3.MoveTowards(
transform.position ,
transform.position+directionToTarget ,
step
);

}
``````