简体   繁体   English

Unity相机通过鼠标输入旋转,如何旋转移动到默认位置?

[英]Unity camera rotate by mouse input, How to rotate the move to the default position?

Camera rotate by mouse input, How to rotate the move to the default position?相机通过鼠标输入旋转,如何旋转移动到默认位置?

public class CameraOrbit : MonoBehaviour
{
    public float turnSpeed = 1.0f;
    public Transform player;
    
    private Vector3 offset;
    
    private void Start()
    {
        offset = new Vector3(0, 2.5f, -5);
    }
    
    private void LateUpdate()
    {
        if (Input.GetMouseButton(0))
        {
            offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
            transform.localPosition = offset;
            transform.LookAt(player.position);
        }
    }
}

This kind of thing moves directly into position and I want to smooth it over.这种东西直接移动到位,我想把它抹平。

public void RevertCamera()
{
    offset = new Vector3(0, 2.5f, -5);
    transform.localPosition = offset;
    transform.LookAt(player.position);
}

I have tried multiple variations of this, but none of them seem to work.我尝试了多种变体,但它们似乎都不起作用。

If you want to achieve smooth transitions.如果要实现平滑过渡。 I would advise you to use Slerp on the Quaternion and interpolate between your 2 rotation points.我建议您在四元数上使用 Slerp 并在您的 2 个旋转点之间进行插值。

Slerp Example: Slerp 示例:

// Interpolates rotation between the rotations "from" and "to"
// (Choose from and to not to be the same as
// the object you attach this script to)

using UnityEngine;
using System.Collections;

public class SlerpExample: MonoBehaviour {
    [SerializeField]private Transform from;
    [SerializeField]private Transform player;

    private bool revertCamera = false;
    private float timeCount = 0.0f;

    private void Update() {
        timeCount = timeCount + Time.deltaTime;

        if (revertCamera) {
            timeCount = 0.0f;
            transform.rotation = Quaternion
                .Slerp(from.rotation, player.rotation, timeCount);

            if (transform.rotation == player.rotation) {
                reverCamera = false;
            }
        }
    }

    private void RevertCamera() {
        revertCamera = true;
    }
}

Slerp 斯勒普

the easiest way is to use the camera's transform's RotateAround(...) method:最简单的方法是使用相机变换的RotateAround(...)方法:

void LateUpdate 
{
    if(Input.GetMouseDown(0))
    {
        float delta = Input.GetAxis("Mouse X") * turnSpeed;
        transform.RotateAround(player.position, Vector3.up, delta);
    }
}

(taken from: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html ) (取自: https : //docs.unity3d.com/ScriptReference/Transform.RotateAround.html

Suggestion: I usually set up a camera rig, if I want to control camera movement focused on a certain target.建议:如果我想控制相机移动集中在某个目标上,我通常会设置一个camera rig。 Though this might be overengineered for a simple RotateAround call.尽管对于简单的 RotateAround 调用,这可能会过度设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM