简体   繁体   English

在 Unity 中鼠标拖动时围绕游戏对象旋转相机

[英]Rotate Camera around a gameObject on Mouse drag in Unity

I want to rotate camera around a gameObject (Say a cube) on a drag of my mouse to simulate a feeling that the gameObject is rotating (Just like we rotate object in scene editor and like in shopping websites).我想在拖动鼠标时围绕游戏对象(比如说一个立方体)旋转相机,以模拟游戏对象正在旋转的感觉(就像我们在场景编辑器中旋转对象以及在购物网站中一样)。

The script below is the one that I'm using.下面的脚本是我正在使用的脚本。 But sometimes the script behaves very weirdly.但有时脚本的行为非常奇怪。 The camera rotates in the opposite direction than the anticipated direction.摄像机沿与预期方向相反的方向旋转。 Why is this happening?为什么会这样? What changes do I need to make in the code to make it work?我需要对代码进行哪些更改才能使其正常工作? Please help.请帮忙。

using UnityEngine;
using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour
{
    public Camera cameraObj;
    public GameObject myGameObj;
    public float speed = 2f;

    void Update()
    {
        RotateCamera();
    }

    void RotateCamera()
    {
        if(Input.GetMouseButton(0))
        {
         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         Vector3.up,
                                         -Input.GetAxis("Mouse X")*speed);

         cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                         Vector3.right,
                                         -Input.GetAxis("Mouse Y")*speed);
        } 

    }
}

I think it is caused by the reference axis.我认为这是由参考轴引起的。

Because you used Vector3.up , Vector3.right , not the camera's one, it wasn't rotated the anticipated direction.因为您使用Vector3.upVector3.right ,而不是相机的那个,所以它没有按照预期的方向旋转。 So, you should change like below.所以,你应该像下面这样改变。

void RotateCamera()
{
    if(Input.GetMouseButton(0))
    {
     cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                     cameraObj.transform.up,
                                     -Input.GetAxis("Mouse X")*speed);

     cameraObj.transform.RotateAround(myGameObj.transform.position, 
                                     cameraObj.transform.right,
                                     -Input.GetAxis("Mouse Y")*speed);
    } 

}

My solution to rotate the camera in Unity with Mouse or Touch我使用鼠标或触摸在 Unity 中旋转相机的解决方案

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class CameraRotator : MonoBehaviour
{
    public Transform target;
    public Camera mainCamera;
    [Range(0.1f, 5f)]
    [Tooltip("How sensitive the mouse drag to camera rotation")]
    public float mouseRotateSpeed = 0.8f;
    [Range(0.01f, 100)]
    [Tooltip("How sensitive the touch drag to camera rotation")]
    public float touchRotateSpeed = 17.5f;
    [Tooltip("Smaller positive value means smoother rotation, 1 means no smooth apply")]
    public float slerpValue = 0.25f; 
    public enum RotateMethod { Mouse, Touch };
    [Tooltip("How do you like to rotate the camera")]
    public RotateMethod rotateMethod = RotateMethod.Mouse;


    private Vector2 swipeDirection; //swipe delta vector2
    private Quaternion cameraRot; // store the quaternion after the slerp operation
    private Touch touch;
    private float distanceBetweenCameraAndTarget;

    private float minXRotAngle = -80; //min angle around x axis
    private float maxXRotAngle  = 80; // max angle around x axis

    //Mouse rotation related
    private float rotX; // around x
    private float rotY; // around y
    private void Awake()
    {
        if (mainCamera == null)
        {
            mainCamera = Camera.main;
        }

        
    }
    // Start is called before the first frame update
    void Start()
    {
        distanceBetweenCameraAndTarget = Vector3.Distance(mainCamera.transform.position, target.position);
    }

    // Update is called once per frame
    void Update()
    {
        if (rotateMethod == RotateMethod.Mouse)
        {
            if (Input.GetMouseButton(0))
            {
                rotX += -Input.GetAxis("Mouse Y") * mouseRotateSpeed; // around X
                rotY += Input.GetAxis("Mouse X") * mouseRotateSpeed;
            }

            if (rotX < minXRotAngle)
            {
                rotX = minXRotAngle;
            }
            else if (rotX > maxXRotAngle)
            {
                rotX = maxXRotAngle;
            }
        }
        else if (rotateMethod == RotateMethod.Touch)
        {
            if (Input.touchCount > 0)
            {
                touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began)
                {
                    //Debug.Log("Touch Began");

                }
                else if (touch.phase == TouchPhase.Moved)
                {
                    swipeDirection += touch.deltaPosition * Time.deltaTime * touchRotateSpeed;
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    //Debug.Log("Touch Ended");
                }
            }

            if (swipeDirection.y < minXRotAngle)
            {
                swipeDirection.y = minXRotAngle;
            }
            else if (swipeDirection.y > maxXRotAngle)
            {
                swipeDirection.y = maxXRotAngle;
            }
        }

    }

    private void LateUpdate()
    {

        Vector3 dir = new Vector3(0, 0, -distanceBetweenCameraAndTarget); //assign value to the distance between the maincamera and the target

        Quaternion newQ; // value equal to the delta change of our mouse or touch position
        if (rotateMethod == RotateMethod.Mouse)
        {
           newQ  = Quaternion.Euler(rotX , rotY, 0); //We are setting the rotation around X, Y, Z axis respectively
        }
        else
        {
            newQ = Quaternion.Euler(swipeDirection.y , -swipeDirection.x, 0);
        }
        cameraRot = Quaternion.Slerp(cameraRot, newQ, slerpValue);  //let cameraRot value gradually reach newQ which corresponds to our touch
        mainCamera.transform.position = target.position + cameraRot * dir;
        mainCamera.transform.LookAt(target.position);

    }

    public void SetCamPos()
    {
        if(mainCamera == null)
        {
            mainCamera = Camera.main;
        }
        mainCamera.transform.position = new Vector3(0, 0, -distanceBetweenCameraAndTarget);
    }

}

To move your cube in the direction of mouse moves change your code like blow:要沿鼠标移动的方向移动立方体,请更改代码,例如:

void RotateCamera()
{
    if (Input.GetMouseButton(0))
    {
        cameraObj.transform.RotateAround(myGameObj.transform.position,
                                        Vector3.up,
                                        Input.GetAxis("Mouse X") * speed);

        cameraObj.transform.RotateAround(myGameObj.transform.position,
                                        Vector3.right,
                                        -Input.GetAxis("Mouse Y") * speed);
    }

}

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

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