S
S
sqtr2021-12-04 13:46:14
Unity
sqtr, 2021-12-04 13:46:14

How to soften camera movement?

When moving the camera using Touch, it does not move smoothly.

private Touch touch;
    public float speed = 0.01f;
    //public float speedFinal = 60f;
    public GameObject mainScriptObject;
    private MainScript mainScript;

    void Start()
    {
        mainScript = mainScriptObject.GetComponent<MainScript>();
    }
    void Update()
    {
        if (Input.touchCount > 0 && !mainScript.isFreezed)
        {
            touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Moved)
            {
                transform.position = new Vector3(
                    Mathf.Clamp(transform.position.x - touch.deltaPosition.x * speed, -100.0f, 100.0f),
                    transform.position.y,
                    Mathf.Clamp(transform.position.z - touch.deltaPosition.y * speed, -100.0f, 100.0f));
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2021-12-04
@ShockWave2048

if (touch.phase == TouchPhase.Moved)
{
    const float SMOOTH = 0.1f;
    var newPos = new Vector3(
    Mathf.Clamp(transform.position.x - touch.deltaPosition.x * speed, -100.0f, 100.0f),
    transform.position.y,
    Mathf.Clamp(transform.position.z - touch.deltaPosition.y * speed, -100.0f, 100.0f));
    transform.position = Vector3.Lerp(transform.position, newPos, SMOOTH);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question