C# Unity3d:按下鼠标中键,围绕屏幕中心旋转相机
我正在做一个RTS游戏,我想支持相机在按下鼠标后围绕屏幕的当前中心旋转,就像任何RTS战略游戏一样 我尝试了unity wiki上显示的MouseOrbitImproved脚本(复制如下),但它需要相机使用的目标对象来旋转。我的问题是,我的目标始终是屏幕的当前中心 你知道如何做到这一点,但只在按下鼠标中键的时候C# Unity3d:按下鼠标中键,围绕屏幕中心旋转相机,c#,unity3d,C#,Unity3d,我正在做一个RTS游戏,我想支持相机在按下鼠标后围绕屏幕的当前中心旋转,就像任何RTS战略游戏一样 我尝试了unity wiki上显示的MouseOrbitImproved脚本(复制如下),但它需要相机使用的目标对象来旋转。我的问题是,我的目标始终是屏幕的当前中心 你知道如何做到这一点,但只在按下鼠标中键的时候 using UnityEngine; using System.Collections; [AddComponentMenu("Camera-Control/Mouse Orbit w
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate ()
{
if (target)
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast (target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
使用UnityEngine;
使用系统集合;
[AddComponentMenu(“摄影机控制/带缩放的鼠标动态观察”)]
公共级鼠标轨道:单行为{
公共转型目标;
公共浮动距离=5.0f;
公共浮点数xSpeed=120.0f;
公共浮动Y速度=120.0f;
公共浮动yMinLimit=-20f;
公共浮动yMaxLimit=80f;
公共浮动距离最小值=0.5f;
公共浮动距离最大值=15f;
私人刚体;
浮动x=0.0f;
浮动y=0.0f;
//用于初始化
无效开始()
{
Vector3角度=transform.eulerAngles;
x=角度y;
y=角度x;
刚体=GetComponent();
//使刚体不改变旋转
if(刚体!=null)
{
刚体旋转=真;
}
}
无效更新()
{
如果(目标)
{
x+=Input.GetAxis(“鼠标x”)*xSpeed*distance*0.02f;
y-=Input.GetAxis(“鼠标y”)*ySpeed*0.02f;
y=抓斗(y,yMinLimit,yMaxLimit);
四元数旋转=四元数欧拉(y,x,0);
距离=数学夹具(距离-输入.GetAxis(“鼠标滚轮”)*5,距离最小,距离最大);
雷卡斯特击中;
if(物理线投射(目标位置、变换位置、出击))
{
距离-=命中距离;
}
矢量3负距离=新矢量3(0.0f,0.0f,-距离);
矢量3位置=旋转*负距离+目标位置;
transform.rotation=旋转;
变换位置=位置;
}
}
公共静态浮子夹持器(浮子角度、浮子最小值、浮子最大值)
{
如果(角度<-360F)
角度+=360F;
如果(角度>360F)
角度-=360F;
返回数学夹具(角度、最小值、最大值);
}
}
在“动态观察”功能中,确定要动态观察的对象,从屏幕中心光线投射光线并围绕碰撞进行动态观察:
public LayerMask groundLayerMask; // mask to choose orbitable layers
...
Ray screenRay = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0f));`
RaycastHit hitInfo;
Physics.Raycast(screenRay, out hitInfo, Mathf.Infinity, groundLayerMask.value);
Vector3 orbitPosition = hitInfo.point;
您可以使用Input.GetMouseButton(2)
检查是否按下了鼠标中键。在动态观察之前,请检查它,并且仅在按住鼠标时检测鼠标输入的更改
在MouseOrbitImproved
代码中,在LateUpdate
方法中可能会出现以下情况:
void LateUpdate ()
{
// Skip mouse input here if middle mouse button is not pressed
if (Input.GetMouseButton(2))
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
Ray screenRay = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0f));`
RaycastHit hitInfo;
Physics.Raycast(screenRay, out hitInfo, Mathf.Infinity, groundLayerMask.value);
Vector3 orbitPosition = hitInfo.point;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + orbitPosition ;
transform.rotation = rotation;
transform.position = position;
}
在类字段中,去掉
公共转换目标
并添加公共层掩码groundLayerMask编码>并在inspector中将其设置为仅与地面的任何层碰撞。尝试从屏幕中心向世界表面投射光线,并使用该位置环绕。谢谢,您是否有机会提供此操作的代码片段?能否请您更具体一些?对不起,这方面我还不太熟悉。如果有一个更完整的解决方案,那就太好了。@AndyBre我编辑了我的答案,以展示答案是如何与MouseOrbitImproved
一起使用的,谢谢@Ruzihm。在您建议的解决方案中,我看不到鼠标按钮按下事件。你知道如何在只按下中间按钮的情况下启动摄像机旋转吗?@AndyBre我编辑了这个问题,以便更清楚地了解这一点,并在我的答案中加入了一点。一切正常。回答得好!非常感谢你的帮助。