- Home /
How to control 2 axes of object in 3d world
Hello, I create a mobile App where you can move an 3D object (with touch controls) on the x and y axis, while the object has an increasing speed on the z axis.
Using Joystick assets and changing the objects velocity depending on the joystick is not working correctly, because if you for example swipe to the right and hold the finger in place, the object keeps moving (because of the Joystick vector).
So basically I want the objects to move exactly like the finger.
I also tried using TouchPhases and the MovePosition function, but I don't know what to put in the z coordinate of the Vector3, so that the objects keeps continuously moving in z-direction.
So here are my tries:
Nr. 1:
player_body.velocity = new Vector3(
joystick.Horizontal * SpeedController.JoystickSensitivity * Time.deltaTime,
joystick.Vertical * SpeedController.JoystickSensitivity * Time.deltaTime,
SpeedController.MovementSpeed);
Nr. 2: (deltaX,Y,Z are floats)
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
deltaZ = transform.position.z;
break;
case TouchPhase.Moved:
player_body.MovePosition(new Vector3(touchPos.x - deltaX, touchPos.y - deltaY, deltaZ));
break;
case TouchPhase.Ended:
player_body.velocity = new Vector3(0, 0, SpeedController.MovementSpeed);
break;
}
}
else
{
player_body.velocity = new Vector3(0, 0, SpeedController.MovementSpeed);
}
Your answer
Follow this Question
Related Questions
Joystick movement problems 0 Answers
How do I move an object with my finger?[C#] 1 Answer
Moving player in direciton camera is facing 1 Answer
store the user touch input 0 Answers