Unity3d Unity相机平滑/减轻阻力

Unity3d Unity相机平滑/减轻阻力,unity3d,camera,drag-and-drop,smooth-scrolling,easing,Unity3d,Camera,Drag And Drop,Smooth Scrolling,Easing,当我拖动相机时,我试图使相机放松或产生惯性,这样当我放下相机时,相机就会轻松就位。我想根据我投掷/拖动相机的力来移动它 这是我用来拖动相机的实际代码,但里面没有平滑的缓和 using UnityEngine; using System.Collections; public class swipeCamera: MonoBehaviour { Vector3 hit_position = Vector3.zero; Vector3 current_position = Vect

当我拖动相机时,我试图使相机放松或产生惯性,这样当我放下相机时,相机就会轻松就位。我想根据我投掷/拖动相机的力来移动它

这是我用来拖动相机的实际代码,但里面没有平滑的缓和

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;
    }
}

谢谢大家!

放松意味着随着时间的推移逐渐行动

因此,此类行为通常通过或其他lerp函数以及
Update
方法实现

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;
    }
}

放松意味着随着时间的推移逐渐行动

因此,此类行为通常通过或其他lerp函数以及
Update
方法实现

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;
    }
}

zwcloud非常感谢你的工作,真是魅力四射!非常感谢你的帮助!!先生,你找到了一个新朋友,我很高兴这对你有帮助。请记住在您能够投票时向上投票。:)嘿,伙计们,迟来的建议,但如果有人再读一遍:)我会使用
Vector3.Lerp()
。这会使摄影机的移动更加平滑,因为它会在最后慢慢减小<代码>transform.position=Vector3.Lerp(transform.position,targetPos,Time.deltaTime*DragSpeed)zwcloud谢谢你这么多,工作得很有魅力!非常感谢你的帮助!!先生,你找到了一个新朋友,我很高兴这对你有帮助。请记住在您能够投票时向上投票。:)嘿,伙计们,迟来的建议,但如果有人再读一遍:)我会使用
Vector3.Lerp()
。这会使摄影机的移动更加平滑,因为它会在最后慢慢减小<代码>transform.position=Vector3.Lerp(transform.position,targetPos,Time.deltaTime*DragSpeed)