- Home /
Touch movement on drag only with speed
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;
}
}
Answer by SadBread · Feb 06, 2021 at 11:19 PM
@Brosiscreations Hi, The bread is here. There are many ways to get the player to move towards the inputted location. The first method is to use the built in unity methods that automatically moves objects to the location. This would include the Vector3.lerp and Vector3.MoveTowards methods.
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
So, here the code would look like this: Make sure to create a new float or variable that is the speed
1)
transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
2)
transform.position = Vector3.MoveTowards(transform.position, newPos, speed * Time.deltaTime);
However, I am assuming that you only want the player to move left and right by dragging left and right. The way you do that is by getting the distance of the Input.X direction subtracted by the vector of the player. Once you do that, then you can determine how far ahead the input location is in front or behind of you.
Set a float in update to the value of Input.x - Player.position.x, to something for example, MovementDirection.
MovementDirection = Input.x - Player.position.x;
With that float you have the options to make the movement gradual based off its value or a discrete value:
What I mean:
rb.velocity += new vector3(MovementDirection * Time.fixedDeltaTime, 0,0);
Or:
if(MovementDirection > 0){
rb.velocity += new Vector3(MovementSpeed * Time.fixedDeltaTime, 0,0);
}
if(MovementDirection < 0){
rb.velocity += new Vector3(-MovementSpeed * Time.fixedDeltaTime, 0,0);
}
I hope this helps! Good luck programming!
Let me just start by saying thank you very much for your answer and sorry for the delay. I really do appreciate your in depth response. I tried straight away to try and figure this out with the help of what you have said but was still just not getting anywhere. I think I'm just not understanding this, so I took a break from trying the touch control. Now I'm back trying to figure this out and I'm thinking that I need to learn more to understand this, but you have given me a few things to think about. Now I'm considering something simpler like button controls until I learn more. Thank you
Your answer

Follow this Question
Related Questions
Movement like in Color Road 2 Answers
Starting drag onMouseEnter 0 Answers
How to instantiate object at touch position? 3 Answers
Dragging an object by touch? 6 Answers
How to increase the acceleration of a falling object? 1 Answer