Y
Y
yungbalykva2021-09-14 16:20:47
C++ / C#
yungbalykva, 2021-09-14 16:20:47

How to speed up the movement speed of a sprite in Unity?

6140a057c9c81197713002.png

I’m making a simple mobile phone on the unit, I added a rat and set a private float speed of 10 for it, I also made it so that it does not go beyond -2.5 and 2.5. The mouse did not go beyond the borders, everything worked, there was also a problem that if I tap to any place, then the sprite will immediately move there, I fiskil. Here is the code

using UnityEngine;

public class MovePlayer : MonoBehaviour
{   
    public Transform player;
    [SerializeField]
    private float speed = 10f;

    void OnMouseDrag () {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        mousePos.x = mousePos.x > 2.5f ? 2.5f : mousePos.x;
        mousePos.x = mousePos.x < -2.5f ? -2.5f : mousePos.x;
        player.position = Vector2.MoveTowards (player.position, 
            new Vector2 (mousePos.x, player.position.y),
            speed = Time.deltaTime);

    }



}


And now this rat is moving very slowly, although in the script the speed is 10, in the value of the script in the unit it is also 10, please tell me what the problem is.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question