- Home /
Question by
jaredweber01 · Jun 22, 2020 at 03:00 PM ·
movementplayertransform.positionmobile devicesmovetowards
Move player between 3 given points only by touching on the left/right side of the screen
I want to move my player between three fix points by touching the left or right side of the screen. My Problem is, that the player moves but only if my finger remains on the screen, as soon as i put it away the player stops and doesn't finish moving to his destination.
My code so far:
public class playerCarMove : MonoBehaviour { public float speed = 3.0f;
private float ScreenWidth;
public Transform leftPos;
public Transform rightPos;
public Transform centralPos;
private void Start()
{
transform.position = centralPos.position;
ScreenWidth = Screen.width;
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).position.x < ScreenWidth / 2)
{
if (transform.position.x < 0)
{
transform.position = Vector2.MoveTowards(transform.position, leftPos.position, speed * Time.deltaTime);
}
if (transform.position.x == 0)
{
transform.position = Vector2.MoveTowards(transform.position, leftPos.position, speed * Time.deltaTime);
}
if (transform.position.x > 0)
{
transform.position = Vector2.MoveTowards(transform.position, centralPos.position, speed * Time.deltaTime);
}
}
if (Input.GetTouch(i).position.x > ScreenWidth / 2)
{
if (transform.position.x > 0)
{
transform.position = Vector2.MoveTowards(transform.position, rightPos.position, speed * Time.deltaTime);
}
if (transform.position.x == 0)
{
transform.position = Vector2.MoveTowards(transform.position, rightPos.position, speed * Time.deltaTime);
}
if (transform.position.x < 0)
{
transform.position = Vector2.MoveTowards(transform.position, centralPos.position, speed * Time.deltaTime);
}
}
}
}
}
Comment
Answer by ShadoX · Jun 24, 2020 at 01:42 PM
The reason for that is that you're only running the movement code, when you're touching the screen. Instead of doing that, you just want to detect a Players touch and then use that to do the movement independently.
like for example:
private Vector3 positionToMoveTo = centralPos.position; // or whatever the starting position is
void Update() {
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).position.x < ScreenWidth / 2)
{
if (transform.position.x < 0)
{
positionToMoveTo = leftPos.position;
}
if (transform.position.x == 0)
{
positionToMoveTo = leftPos.position;
}
if (transform.position.x > 0)
{
positionToMoveTo = centralPos.position;
}
}
if (Input.GetTouch(i).position.x > ScreenWidth / 2)
{
if (transform.position.x > 0)
{
positionToMoveTo = rightPos.position;
}
if (transform.position.x == 0)
{
positionToMoveTo = rightPos.position;
}
if (transform.position.x < 0)
{
positionToMoveTo = centralPos.position;
}
}
}
if(transform.position != positionToMoveTo){
transform.position = Vector2.MoveTowards(transform.position, positionToMoveTo, speed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Using MoveTowards on button click 0 Answers
Error Vector3 does not contain MoveTowards 3 Answers