unity-game-enginecameradrag-and-dropsmooth-scrollingeasing

Unity camera smooth/easing drag


I´m trying to make an easing or inertia when dragging the camera so when i drop the camera it eases into place. I'd like to move the camera based on the force i throw/drag it.

This is the actual code i´m using to drag the camera but there is no smooth easing in it.

    using UnityEngine;
    using System.Collections;

    public class swipeCamera: MonoBehaviour
    {
        Vector3 hit_position = Vector3.zero;
        Vector3 current_position = Vector3.zero;
        Vector3 camera_position = Vector3.zero;
        float z = 0.0f;

        // Use this for initialization
        void Start()
        {
        }

        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                hit_position = Input.mousePosition;
                camera_position = transform.position;

            }
            if (Input.GetMouseButton(0))
            {
                current_position = Input.mousePosition;
                LeftMouseDrag();
            }
        }

        void LeftMouseDrag()
        {
            // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
            // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
            current_position.z = hit_position.z = camera_position.y;

            // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
            // anyways.  
            Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

            // Invert direction to that terrain appears to move with the mouse.
            direction = direction * -1;

            Vector3 position = camera_position + direction;

            transform.position = position;
        }
    }

Thank you!


Solution

  • Easing means moving gradually as time goes on.

    So such behaviours are usually implemented with Vector.MoveTowards or other lerp functions and Time.deltaTime in the Update method.

    using UnityEngine;
    using System.Collections;
    
    public class swipeCamera: MonoBehaviour
    {
        public float speed = 2.0f;//easing speed
    
        Vector3 hit_position = Vector3.zero;
        Vector3 current_position = Vector3.zero;
        Vector3 camera_position = Vector3.zero;
        float z = 0.0f;
    
        bool flag = false;
        Vector3 target_position;
    
        void Start()
        {
        }
    
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                hit_position = Input.mousePosition;
                camera_position = transform.position;
            }
    
            if (Input.GetMouseButton(0))
            {
                current_position = Input.mousePosition;
                LeftMouseDrag();
                flag = true;
            }
            
            if(flag)
            {
                transform.position = Vector3.MoveTowards(transform.position, target_position, Time.deltaTime*speed);
                if(transform.position == target_position)//reached?
                {
                    flag = false;// stop moving
                }
            }
        }
    
        void LeftMouseDrag()
        {
            // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
            // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
            current_position.z = hit_position.z = camera_position.y;
    
            // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
            // anyways.  
            Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
    
            // Invert direction to that terrain appears to move with the mouse.
            direction = direction * -1;
    
            target_position = camera_position + direction;
        }
    }