I’m completely lost so I hope someone can help me.
I’ve got a 2D level, I need the player to move left and right only by dragging. How do I do this without teleporting and with a set speed?
This is the first time I’m attempting touch controls, I’m still quite new to unity and I’m just not getting it. I’ve spent the majority of the day looking at questions and watching videos, I’m still no closer to figuring it out. I’ve tried different things with no luck, I’m finding it hard to understand.
I’ve attached what I have at the moment, this drags left and right but does nothing with speed, if I touch a different part of the screen the player will just teleport there.
Any help would be much appreciated.
public float moveSpeed;
void Move()
{
if (Input.touchCount > 0)
{
if (GameManager.instance.speedUpPressed == true)
{
Vector3 screenPos = Input.mousePosition;
screenPos.z = 10.0f;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
Vector3 newPos = transform.position += Vector3.right * moveSpeed * 1.5f * Time.deltaTime;
newPos.x = worldPos.x;
transform.position = newPos;
}
else
{
{
//inputX = Input.touchCount;
Vector3 screenPos = Input.mousePosition;
screenPos.z = 10.0f;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
Vector3 newPos = transform.position += Vector3.right * moveSpeed * Time.deltaTime;
newPos.x = worldPos.x;
transform.position = newPos;
}
}