- Home /
Question by
noahbianchi · Jul 06, 2020 at 10:36 AM ·
programmingplayer movement
how to move a gameobject only on the x axis?
I am trying to make a mobile game... My problem is that it now moves on the x and y axis. I only need the x axis, but I don't know what to change to just move it on the x axis.
// Update is called once per frame
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
touchposition = Camera.main.ScreenToWorldPoint(touch.position);
touchposition.z = 0;
direction = (touchposition - transform.position);
rb.velocity = new Vector2(direction.x, direction.y) * movespeed;
if (touch.phase == TouchPhase.Ended)
rb.velocity = Vector2.zero;
}
}
}
Comment
Best Answer
Answer by Macarona_Virus · Jul 06, 2020 at 11:05 AM
@noahbianhi in the 10th line simply say: rb.velocity = new Vector2(direction.x, 0f)* moveSpeed;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Platformer problems 1 Answer
Tightening up turning radius with AddRelativeForce (or other options) 1 Answer
Player Movement in unity3d 1 Answer
Socket programming in unity 3 Answers