Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 使用鼠标在Unity中拖动,围绕游戏对象旋转相机_Unity3d - Fatal编程技术网

Unity3d 使用鼠标在Unity中拖动,围绕游戏对象旋转相机

Unity3d 使用鼠标在Unity中拖动,围绕游戏对象旋转相机,unity3d,Unity3d,我想拖动鼠标,围绕游戏对象(比如立方体)旋转相机,以模拟游戏对象正在旋转的感觉(就像我们在场景编辑器和购物网站中旋转对象一样) 下面的脚本就是我正在使用的脚本。但有时脚本的行为非常怪异。相机的旋转方向与预期方向相反。为什么会这样?我需要对代码进行哪些更改才能使其正常工作?请帮忙 using UnityEngine; using System.Collections; public class ExampleBehaviourScript : MonoBehaviour { public

我想拖动鼠标,围绕游戏对象(比如立方体)旋转相机,以模拟游戏对象正在旋转的感觉(就像我们在场景编辑器和购物网站中旋转对象一样)

下面的脚本就是我正在使用的脚本。但有时脚本的行为非常怪异。相机的旋转方向与预期方向相反。为什么会这样?我需要对代码进行哪些更改才能使其正常工作?请帮忙

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

    }
}

我认为这是由参考轴引起的

因为您使用的是
Vector3.up
Vector3.right
,而不是相机的,所以它没有按预期方向旋转。 所以,你应该像下面这样改变

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

}

我认为这是由参考轴引起的

因为您使用的是
Vector3.up
Vector3.right
,而不是相机的,所以它没有按预期方向旋转。 所以,你应该像下面这样改变

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

}

要沿鼠标移动的方向移动立方体,请更改代码,如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);
    }

}

要沿鼠标移动的方向移动立方体,请更改代码,如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);
    }

}
我的解决方案是使用鼠标或触摸键统一旋转相机
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
公共级摄影师:单一行为
{
公共转型目标;
公共摄像机;
[范围(0.1f,5f)]
[工具提示(“鼠标拖动对摄影机旋转的敏感程度”)]
公共浮动鼠标速度=0.8f;
[范围(0.01f,100)]
[工具提示(“触摸拖动对摄影机旋转的敏感程度”)]
公共浮动触摸旋转速度=17.5f;
[工具提示(“较小的正值表示旋转更平滑,1表示不平滑应用”)]
公共浮点数=0.25f;
公共枚举旋转方法{鼠标,触摸};
[工具提示(“您希望如何旋转摄影机”)]
public RotateMethod RotateMethod=RotateMethod.Mouse;
私有向量2 SwipDirection;//滑动增量向量2
私有四元数cameraRot;//在slerp操作后存储四元数
私人接触;
mera和target之间的私有浮动距离;
私有浮点minXRotAngle=-80;//围绕x轴的最小角度
专用浮点maxXRotAngle=80;//绕x轴的最大角度
//鼠标旋转相关
私有浮点rotX;//在x附近
私有浮动旋转;//围绕旋转
私人空间
{
如果(mainCamera==null)
{
mainCamera=Camera.main;
}
}
//在第一帧更新之前调用Start
void Start()
{
MERA和目标之间的距离=矢量3.距离(main camera.transform.position,target.position);
}
//每帧调用一次更新
无效更新()
{
if(rotateMethod==rotateMethod.Mouse)
{
if(输入。GetMouseButton(0))
{
rotX+=-Input.GetAxis(“鼠标Y”)*鼠标状态速度;//大约X
rotY+=Input.GetAxis(“鼠标X”)*鼠标速度;
}
if(旋转角度<最小旋转角度)
{
rotX=最小旋转角度;
}
else if(旋转>最大旋转角度)
{
rotX=最大旋转角度;
}
}
else if(rotateMethod==rotateMethod.Touch)
{
如果(Input.touchCount>0)
{
touch=Input.GetTouch(0);
if(touch.phase==TouchPhase.start)
{
//Log(“触摸开始”);
}
else if(touch.phase==TouchPhase.Moved)
{
SwipDirection+=touch.deltaPosition*Time.deltaTime*touch rotateSpeed;
}
否则如果(touch.phase==TouchPhase.end)
{
//调试日志(“触摸结束”);
}
}
if(旋转方向ymaxXRotAngle)
{
swipeDirection.y=最大旋转角度;
}
}
}
私有更新()
{
Vector3 dir=新的Vector3(0,0,-摄像机和目标之间的距离);//为主摄像机和目标之间的距离赋值
四元数newQ;//值等于鼠标或触摸位置的增量变化
if(rotateMethod==rotateMethod.Mouse)
{
newQ=Quaternion.Euler(rotX,rotY,0);//我们分别设置围绕X,Y,Z轴的旋转
}
其他的
{
newQ=四元数.Euler(swipeDirection.y,-swipeDirection.x,0);
}
cameraRot=Quaternion.Slerp(cameraRot,newQ,slerpValue);//让cameraRot值逐渐达到与我们的触摸相对应的newQ
mainCamera.transform.position=target.position+cameraRot*dir;
主摄像机。变换。注视(目标。位置);
}
公共空间
{
如果(mainCamera==null)
{
mainCamera=Camera.main;
}
main camera.transform.position=新矢量3(0,0,-摄像机和目标之间的距离);
}
}
我用鼠标或触摸键统一旋转相机的解决方案
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
公共级摄影师:单一行为
{
公共转型目标;
公共摄像机;
[范围(0.1f,5f)]
[工具提示(“鼠标拖动对摄影机旋转的敏感程度”)]
公共浮动鼠标速度=0.8f;
[范围(0.01f,100)]
[工具提示(“触摸拖动对摄影机旋转的敏感程度”)]
公共浮动触摸旋转速度=17.5f;
[工具提示(“较小的正值表示旋转更平滑,1表示不平滑应用”)]
公共浮点数=0.25f;
公共枚举旋转方法{Mo