- Home /
How to rotate an object using touch controls
Sorry if this question is silly. Still a beginner :I
I made a C# script and attached it to my player (gameobject) so it can rotate around the Z axis using the A & D keyboard controls. However I want to make it so you can do the same on mobile but have it be so the player can drag touch there screens left and right to rotate the gameobject around the Z axis. Here is the current code below.
public float rotatespeed = 10f;
void Update()
{
if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.back, rotatespeed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.back, -turnspeed * Time.deltaTime);
}
So you want to make something like a virtual horizontal only joystick? or more like if you touch the left part of the phone it will turn left and if you touch right it would go right?
Answer by developeration1 · Dec 02, 2019 at 07:04 PM
First of all, check the documentation for mobile device input at https://docs.unity3d.com/Manual/MobileInput.html .
Now, there are many ways you can do this, but try using cases, something like this example provided by Unity: https://docs.unity3d.com/ScriptReference/TouchPhase.Ended.html
In the Update method, check that you have at least 1 finger in the screen in the first place with Input.touchCount
, then by taking the first finger that is in the touchscreen with Input.GetTouch(0)
check its TouchPhase
with a switch
:
If the phase is
TouchPhase.Began
establish the starting positionIf the phase is
TouchPhase.Moved
check how much the position has changed; if it's more than the starting position, move right, else, if is less than starting position move left.Finally if the phase is
TouchPhase.Exit
just send aDebug.Log
message that the finger is no longer in the screen (this one is optional).
After all, modifying your code, you'll get something like this:
public float rotatespeed = 10f;
private float _startingPosition;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
_startingPosition = touch.position.x;
break;
case TouchPhase.Moved:
if (startingPosition > touch.position.x)
{
transform.Rotate(Vector3.back, -turnspeed * Time.deltaTime);
}
else if (startingPosition < touch.position.x)
{
transform.Rotate(Vector3.back, rotatespeed * Time.deltaTime);
}
break;
case TouchPhase.Ended:
Debug.Log("Touch Phase Ended.")
break;
}
}
}
}
That would be the code, assuming that your's already work with keyboard input. Hope that helps!
Awesome @developeration1 , I would like to add this case in switch statement:
case TouchPhase.Stationary:
startingPosition = touch.position.x;
break;
Answer by unknownsk · Feb 08 at 01:19 PM
if (Input.touchCount > 0 && CanRotate)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startingPosition = touch.position.x;
break;
case TouchPhase.Moved:
if (startingPosition > touch.position.x)
{
RobloxModel.transform.Rotate(Vector3.up, rotatespeed * Time.deltaTime);
}
else if (startingPosition < touch.position.x)
{
RobloxModel.transform.Rotate(Vector3.up, -rotatespeed * Time.deltaTime);
}
break;
case TouchPhase.Ended:
Debug.Log("Touch Phase Ended.");
break;
case TouchPhase.Stationary:
startingPosition = touch.position.x;
break;
}
}
Answer by nanditho · Mar 11 at 05:28 AM
If I want to rotate in any axis (X, Y, Z) depends on rotation of camera is it possible?