ScreenToWorldPoint Horizontal movement issue

With this code the movement is working, but when i’m starting the game on IOS
If the player is in the left corner (-2.5f) and I tap on the right corner (2.5f) the player teleport to there.
I want to that the movement is smooth, I understand I need to use Vector3.MoveTowards but can’t understand how, any way I try just make it full of bug.
If you can explain me how it would be great, Thanks! :slight_smile:

void Update()
    {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
        float moveHorizontal = Input.GetAxis("Horizontal");
        transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + moveHorizontal, -2.5f, 2.5f),
            gameObject.transform.position.y, gameObject.transform.position.z), diretionalSpeed * Time.deltaTime);
#endif
        GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
        //MONILE CONTROLS
        Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 5f));
        if (Input.touchCount > 0)
        {
          transform.position = new Vector3 (touch.x, transform.position.y, transform.position.z);
        }
    }

Well, I’m sort of new at this, but I think I can help with this one.
In your place I would create a variable to store the target position first instead of sending that information in the transform.position.

So for example:

“private Vector3 targetLocation;”

on Start():
targetLocation = new Vetor3 (0, 0, 0);

on Update():

if (Input.touchCount>0)
{
targetLocation.x = touch.x;
transform.position = Vetctor3.MoveTowards (transform.position, targetLocation, 2.0f);
}

So MoveTowards is just a vector that is going to add to your current transform.position vector… but it already calculates the direction of the movement based on the current position (first argument, “transform.position”) and it’s target location (second argument, "targetLocation). The third argument (I used 2.0f on the example) is the spead you wish to use to move towards the location.

Hope that helps.
Cheers.

Hey, thanks for your quick respond, now I understand how I can use the MoveTowards in the future
It’s working the only problem is when I play the game and touching the screen and slide it the player return back to his start position and freeze until I stop touch the screen (it’s an endless runner).

Thanks again :slight_smile:
I

I’m glad it helped!

About the new problem, my first hypothesis is that you put the “targetLocation = new Vetor3 (0, 0, 0);” in the update function… which would be called each tick. If you put it in the “Start()” function it will run once when the game opens… So in the “update()” function you can update targetLocation only when the player touches the screen. One thing you can do is create a touch counter… and only update the vector when the touch count goes up. Something like:

"if(touchCounter < input.touchCount)
{
touchCounter = input.TouchCount;
targetLocation.x = touch.x;
transform.position = Vetctor3.MoveTowards (transform.position, targetLocation, 2.0f);
}

But again, I’m not sure if that’s the problem. If if isn’t post your code and I’ll try to help (or maybe someone else will if I can’t find the problem).

Cheers!

“targetLocation = new Vetor3 (0, 0, 0);”
Yeah I wrote it inside the start function and it still dont follow the character.

Here’s a video, you can see what actully happen

https://www.youtube.com/watch?v=GBoclFh0Las

and here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public GameObject sceneManager;
    public float playerSpeed = 500;
    public float diretionalSpeed = 20;
    public AudioClip scoreUp;
    public AudioClip damage;

    private Vector3 targetLocation;

    // Start is called before the first frame update
    void Start()
    {
        targetLocation = new Vector3(0, 0, 0);
    }

    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
        float moveHorizontal = Input.GetAxis("Horizontal");
        transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + moveHorizontal, -2.5f, 2.5f),
            gameObject.transform.position.y, gameObject.transform.position.z), diretionalSpeed * Time.deltaTime);
#endif
        GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
        //MONILE CONTROLS
        Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 5f));
        if (Input.touchCount > 0)
        {
            targetLocation.x = touch.x;
            transform.position = Vector3.MoveTowards(transform.position, targetLocation, 2.0f);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "scoreup")
        {
            GetComponent<AudioSource>().PlayOneShot(scoreUp, 1f);
        }

        if (other.gameObject.tag == "triangle")
        {
            GetComponent<AudioSource>().PlayOneShot(damage, 0.5f);
            sceneManager.GetComponent<App_Initialize>().GameOver();
        }

    }
}

Thanks :slight_smile:

okay I’ve figured it out I think
it’s always reset the position of target location to zero because the targetLocation is 0 0 0 (targetLocation = new Vector3(0, 0, 0):wink:
So I found a good solution and stored the target location as targetLocation + the currently position.
anyway its working now!
Is it supposed to work in delay? once I click on the right side for example, it takes time for it to perform the action, its work in delay, the movement is smooth (not telporting) but kinda slow and feel “stuck”.

If you have another solution please let me know, I’m new in unity so it’s difficult for me lol.

here’s the code

 transform.position = Vector3.MoveTowards(transform.position, targetLocation + transform.position, Time.deltaTime * diretionalSpeed );

I’m glad that you were able to work that out! I’m kind of new in this too.
Well, about being slow, you can try rising the directional speed from 20 to 30 or something.
One alternative is to make it accelerate… For example, start with directional speed 10 and while the screen is touched, increase the directional speed (directional speed += 0.2f) or something.
Though if you do this, you’ll want to reset the directional speed once the ball position == target position.