Home > Back-end >  How to MoveTowards on the x only?
How to MoveTowards on the x only?

Time:02-24

transform.position = Vector3.MoveTowards(transform.position,
                        targetTransform.position, Time.deltaTime * movementSpeed);

I want to move the transform towards a target but only on the x.

CodePudding user response:

Use Mathf.MoveTowards on the x:

Vector3 newPos = transform.position;
newPos.x = Mathf.MoveTowards(newPos.x, targetTransform.position.x, 
        Time.deltaTime * movementSpeed);
transform.position = newPos;
  • Related