C# 如何解决统一移动第三个人控制器时的口吃问题?
当我在整个场景中移动我的角色时,我会口吃。只有当我移动角色时。如果角色站着不动就好了。一旦我用了W S D A键,它就会口吃 我在一个答案中读到,之前有人问过,相机的处理部分应该在最新更新功能中。我在这个脚本中有一个LateUpdate函数,所以我不确定问题出在哪里 2013年的答案是“将你的相机处理代码放在最新更新中。这样,你将在更新方法中移动你的玩家对象,相机将在最新更新方法中跟踪他。当我的相机跟随代码和角色移动代码都发生在更新中时,我遇到了同样的问题。” 在spector的主摄像头中,我有一个c#格式的CameraScript:C# 如何解决统一移动第三个人控制器时的口吃问题?,c#,unity3d,unityscript,unity5,C#,Unity3d,Unityscript,Unity5,当我在整个场景中移动我的角色时,我会口吃。只有当我移动角色时。如果角色站着不动就好了。一旦我用了W S D A键,它就会口吃 我在一个答案中读到,之前有人问过,相机的处理部分应该在最新更新功能中。我在这个脚本中有一个LateUpdate函数,所以我不确定问题出在哪里 2013年的答案是“将你的相机处理代码放在最新更新中。这样,你将在更新方法中移动你的玩家对象,相机将在最新更新方法中跟踪他。当我的相机跟随代码和角色移动代码都发生在更新中时,我遇到了同样的问题。” 在spector的主摄像头中,我有
使用UnityEngine;
使用系统集合;
公共类CameraScript:MonoBehavior{
公共转型目标卢卡特;
公共浮动距离=5.0f;
公共浮动距离最小值=3.0f;
公共浮动距离最大值=10.0f;
私人浮动鼠标=0.0f;
私人浮动鼠标=0.0f;
专用浮球启动距离=0.0f;
私人浮动期望距离=0.0f;
公共浮动X_鼠标灵敏度=5.0f;
公共浮动Y_鼠标灵敏度=5.0f;
公共浮动鼠标滚轮灵敏度=5.0f;
公共浮动Y_MinLimit=-40.0f;
公共浮动Y_MaxLimit=80.0f;
公共浮动距离平滑=0.05f;
专用浮动速度距离=0.0f;
专用向量3 desiredPosition=向量3.0;
公共浮动X_平滑=0.05f;
公共浮子Y_平滑=0.1f;
专用浮点数=0.0f;
专用浮点数=0.0f;
私人浮动幅度=0.0f;
专用矢量3位置=矢量3.0;
游标锁定模式wantedMode;
无效开始(){
距离=数学夹具(距离、距离最小值、距离最大值);
起始距离=距离;
重置();
SetCursorState();
OnGUI();
}
无效更新(){
if(TargetLookAt==null)
返回;
HandlePlayerInput();
计算出所需的位置();
UpdatePosition();
}
无效HandlePlayerInput(){
浮动死区=0.01f;//鼠标滚轮死区
//if(输入。GetMouseButton(1))
//{
mouseX+=Input.GetAxis(“鼠标X”)*X_鼠标灵敏度;
mouseY-=Input.GetAxis(“鼠标Y”)*Y\u鼠标灵敏度;
//}
//这就是mouseY受限制的地方-Helper脚本
mouseY=抓斗(mouseY,Y_MinLimit,Y_MaxLimit);
//获取鼠标滚轮输入
if(Input.GetAxis(“鼠标滚轮”)<-deadZone | | Input.GetAxis(“鼠标滚轮”)>deadZone)
{
desiredDistance=Mathf.Clamp(距离-(Input.GetAxis(“鼠标滚轮”)*鼠标滚轮灵敏度),
距离最小值、距离最大值);
}
}
void计算出的所需位置(){
//评估距离
距离=数学平滑阻尼(距离、期望距离、参考速度距离、距离平滑);
//计算所需位置->注意:鼠标输入反转以与世界空间轴对齐
desiredPosition=计算位置(鼠标、鼠标、距离);
}
矢量3计算位置(浮动旋转X、浮动旋转Y、浮动距离){
矢量3方向=新矢量3(0,0,-距离);
四元数旋转=四元数.Euler(旋转X,旋转Y,0);
返回TargetLookAt.position+(旋转*方向);
}
void UpdatePosition(){
浮点位置x=数学平滑阻尼(位置x,所需位置x,参考水平x,x_平滑);
浮点位置y=数学平滑阻尼(位置y,期望位置y,参考,y_平滑);
浮动位置z=数学平滑阻尼(位置z,所需位置z,参考标高z,X_平滑);
位置=新矢量3(posX、posY、posZ);
变换位置=位置;
transform.LookAt(TargetLookAt);
}
无效重置(){
mouseX=0;
mouseY=10;
距离=启动距离;
期望距离=距离;
}
浮动夹持器(浮动角度、浮动最小值、浮动最大值){
同时(角度<-360 | |角度>360)
{
如果(角度<-360)
角度+=360;
如果(角度>360)
角度-=360;
}
返回数学夹具(角度、最小值、最大值);
}
//应用请求的游标状态
void SetCursorState()
{
Cursor.lockState=wantedMode;
//锁定时隐藏光标
Cursor.visible=(CursorLockMode.Locked!=wantedMode);
}
void OnGUI()
{
GUILayout.begintical();
//按escape键释放光标
if(Input.GetKeyDown(KeyCode.Escape))
Cursor.lockState=wantedMode=CursorLockMode.None;
开关(游标锁定状态){
案例游标锁定模式。无:
GUILayout.Label(“光标正常”);
if(GUILayout.Button(“锁定光标”))
wantedMode=光标锁定模式。已锁定;
if(GUILayout.Button(“限制光标”))
wantedMode=游标锁定模式。受限;
打破
案例游标锁定模式。受限:
GUILayout.Label(“光标被限制”);
if(GUILayout.Button(“锁定光标”))
wantedMode=光标锁定模式。已锁定;
if(GUILayout.Button(“释放光标”))
wantedMode=游标锁定模式。无;
打破
案例游标锁定模式。锁定:
GUILayout.Label(“光标已锁定”);
if(GUILayout.Button(“解锁光标”))
wantedMode=游标锁定模式。无;
if(GUILayout.Button(“限制光标”))
wantedMode=游标锁定模式。受限;
打破
}
GUILayout.EndVertical();
SetCursorState();
}
}
理想情况下,永远不要使用最新更新。这不是“真正的解决方案”,
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public Transform TargetLookAt;
public float Distance = 5.0f;
public float DistanceMin = 3.0f;
public float DistanceMax = 10.0f;
private float mouseX = 0.0f;
private float mouseY = 0.0f;
private float startingDistance = 0.0f;
private float desiredDistance = 0.0f;
public float X_MouseSensitivity = 5.0f;
public float Y_MouseSensitivity = 5.0f;
public float MouseWheelSensitivity = 5.0f;
public float Y_MinLimit = -40.0f;
public float Y_MaxLimit = 80.0f;
public float DistanceSmooth = 0.05f;
private float velocityDistance = 0.0f;
private Vector3 desiredPosition = Vector3.zero;
public float X_Smooth = 0.05f;
public float Y_Smooth = 0.1f;
private float velX = 0.0f;
private float velY = 0.0f;
private float velZ = 0.0f;
private Vector3 position = Vector3.zero;
CursorLockMode wantedMode;
void Start (){
Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
startingDistance = Distance;
Reset();
SetCursorState();
OnGUI();
}
void LateUpdate (){
if (TargetLookAt == null)
return;
HandlePlayerInput();
CalculateDesiredPosition();
UpdatePosition();
}
void HandlePlayerInput (){
float deadZone= 0.01f; // mousewheel deadZone
//if (Input.GetMouseButton(1))
//{
mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
//}
// this is where the mouseY is limited - Helper script
mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit);
// get Mouse Wheel Input
if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
{
desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity),
DistanceMin, DistanceMax);
}
}
void CalculateDesiredPosition (){
// Evaluate distance
Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velocityDistance, DistanceSmooth);
// Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
Vector3 CalculatePosition ( float rotationX , float rotationY , float distance ){
Vector3 direction = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + (rotation * direction);
}
void UpdatePosition (){
float posX= Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
float posY= Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
float posZ= Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
position = new Vector3(posX, posY, posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
void Reset (){
mouseX = 0;
mouseY = 10;
Distance = startingDistance;
desiredDistance = Distance;
}
float ClampAngle ( float angle , float min , float max ){
while (angle < -360 || angle > 360)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
// Apply requested cursor state
void SetCursorState ()
{
Cursor.lockState = wantedMode;
// Hide cursor when locking
Cursor.visible = (CursorLockMode.Locked != wantedMode);
}
void OnGUI ()
{
GUILayout.BeginVertical ();
// Release cursor on escape keypress
if (Input.GetKeyDown (KeyCode.Escape))
Cursor.lockState = wantedMode = CursorLockMode.None;
switch (Cursor.lockState) {
case CursorLockMode.None:
GUILayout.Label ("Cursor is normal");
if (GUILayout.Button ("Lock cursor"))
wantedMode = CursorLockMode.Locked;
if (GUILayout.Button ("Confine cursor"))
wantedMode = CursorLockMode.Confined;
break;
case CursorLockMode.Confined:
GUILayout.Label ("Cursor is confined");
if (GUILayout.Button ("Lock cursor"))
wantedMode = CursorLockMode.Locked;
if (GUILayout.Button ("Release cursor"))
wantedMode = CursorLockMode.None;
break;
case CursorLockMode.Locked:
GUILayout.Label ("Cursor is locked");
if (GUILayout.Button ("Unlock cursor"))
wantedMode = CursorLockMode.None;
if (GUILayout.Button ("Confine cursor"))
wantedMode = CursorLockMode.Confined;
break;
}
GUILayout.EndVertical ();
SetCursorState ();
}
}