Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

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
C# 父对象沿1轴的单位旋转_C#_Unity3d - Fatal编程技术网

C# 父对象沿1轴的单位旋转

C# 父对象沿1轴的单位旋转,c#,unity3d,C#,Unity3d,我正在开发一个游戏,我目前的障碍是相机的旋转。我想用鼠标控制摄像机,当摄像机转动时,我也想旋转播放器。但是,我使用的代码会将播放器对象与播放器一起完全旋转,导致播放器像这样旋转: 玩家向前旋转不仅看起来很奇怪,而且会导致地形剪裁问题。如何使我的代码仅沿一个轴旋转播放器,同时允许相机沿任何轴旋转(以允许观众环视四周,而无需向上或向下旋转播放器对象) 这是旋转播放器的代码: Quaternion QT = Quaternion.Euler(_LocalRotation.y, _LocalRotat

我正在开发一个游戏,我目前的障碍是相机的旋转。我想用鼠标控制摄像机,当摄像机转动时,我也想旋转播放器。但是,我使用的代码会将播放器对象与播放器一起完全旋转,导致播放器像这样旋转:

玩家向前旋转不仅看起来很奇怪,而且会导致地形剪裁问题。如何使我的代码仅沿一个轴旋转播放器,同时允许相机沿任何轴旋转(以允许观众环视四周,而无需向上或向下旋转播放器对象)

这是旋转播放器的代码:

Quaternion QT = Quaternion.Euler(_LocalRotation.y, _LocalRotation.x, 0);
    this._XForm_Parent.rotation = Quaternion.Lerp(this._XForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);

    if (this._XForm_Camera.localPosition.z != this._CameraDistance * -1f)
    {
        this._XForm_Camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(this._XForm_Camera.localPosition.z, this._CameraDistance * -1f, Time.deltaTime * ScrollDampening));
    }
这是我正在使用的完整脚本:

using System.Collections;
使用System.Collections.Generic; 使用UnityEngine

公共类CameraOrbit:单一行为 {

protectedtransform\u XForm\u摄像机;
受保护的变换_变换_父项;
受保护向量3\u局部旋转;
受保护浮子_camereditance=10f;
公共浮动鼠标灵敏度=4f;
公众敏感度=2f;
公共浮子轨道阻尼=10f;
公共浮动滚动阻尼=6f;
公共布尔值CameradEnabled=假;
//用于初始化
void Start()
{
this.\u XForm\u Camera=this.transform;
this.\u XForm\u Parent=this.transform.Parent;
}
void LateUpdate()
{
if(Input.GetKeyDown(KeyCode.LeftShift))
CameraRadiabled=!CameraRadiabled;
如果(!CameradEnabled)
{
//基于鼠标坐标的摄影机旋转
if(Input.GetAxis(“鼠标X”)!=0 | | Input.GetAxis(“鼠标Y”)!=0)
{
_LocalRotation.x+=Input.GetAxis(“鼠标x”)*鼠标灵敏度;
_LocalRotation.y+=Input.GetAxis(“鼠标y”)*鼠标灵敏度;
//将y轴旋转夹紧到地平线,不要在顶部翻转
如果(_LocalRotation.y<0f)
_LocalRotation.y=0f;
否则如果(_LocalRotation.y>90f)
_LocalRotation.y=90f;
}
//从鼠标滚轮缩放输入
if(Input.GetAxis(“鼠标滚轮”)!=0f)
{
float ScrollAmount=Input.GetAxis(“鼠标滚轮”)*滚动灵敏度;
ScrollAmount*=(此._CameradInstance*0.3f);
此参数为.\u CameradInstance+=ScrollAmount*-1f;
此.\u CameradInstance=数学夹具(此.\u CameradInstance,1.5f,100f);
}
}
//实际摄影机装备变换
四元数QT=Quaternion.Euler(_LocalRotation.y,_LocalRotation.x,0);
this.\u XForm\u Parent.rotation=Quaternion.Lerp(this.\u XForm\u Parent.rotation,QT,Time.deltaTime*轨道阻尼);
if(this.\u XForm\u Camera.localPosition.z!=this.\u cameradInstance*-1f)
{
this.\u XForm\u Camera.localPosition=new Vector3(0f,0f,Mathf.Lerp(this.\u XForm\u Camera.localPosition.z,this.\u cameradInstance*-1f,Time.deltaTime*滚动阻尼));
}
}
}

我的继承人的照片: (请注意,Player是一个空对象,包含我的所有脚本和物理组件,而GFXs组件只是模型,动画组件没有任何物理组件。)


听起来,你想要的基本上是围绕播放器的局部Y轴旋转播放器,直到它朝向与相机相同的方向。您可以使用向量3.SignedAngle(符号角)并沿您关心的轴传递,来确定事物是否朝向同一方向。下面是一些示例代码,可以帮助您继续

float turnThresholdDegrees = 15;
float playerTurnSpeed = 1;

// Get delta angle in degrees on the Y axis between the direction player is facing and the direction camera is facing
float angleDelta = Vector3.SignedAngle(player.transform.forward, camera.transform.forward, Vector3.up);

// Calculate a rotation speed, clamping it to 0 when the angle delta is < some threshold
float rotateSpeed = Mathf.Abs(angleDelta) > turnThresholdDegrees ? Mathf.Sign(angleDelta) * playerTurnSpeed : 0;

// Rotate the player around its local Y axis by the rotation speed
player.transform.Rotate(0, rotateSpeed * Time.deltaTime, 0, Space.Self);
float-turnstresholdDegrees=15;
浮动播放机转速=1;
//在Y轴上,在播放器所面对的方向和相机所面对的方向之间获取以度为单位的增量角度
float angle delta=Vector3.SignedAngle(player.transform.forward,camera.transform.forward,Vector3.up);
//计算转速,当角度增量<某个阈值时将其钳制为0
浮动旋转速度速度=最大Abs(角度增量)>旋转阈值度?数学符号(角度增量)*播放速度:0;
//按旋转速度围绕播放器的局部Y轴旋转播放器
player.transform.Rotate(0,rotateSpeed*Time.deltaTime,0,Space.Self);

line float rotateSpeed=Mathf.Abs(angleDelta)>turnstresholdDegrees:Mathf.Sign(angleDelta)*播放器转速:0;抛出一堆错误。你能解释一下我如何使用这个代码使播放器旋转(Y轴)与摄像机旋转(Y轴)匹配吗?有什么错误?我在没有编译器的情况下写了这篇文章,所以可能会有一些拼写错误。这应该是相当多的下拉式兼容,您只需要更改东西的名称,以引用您的相机/播放器转换。因此,在您当前尝试旋转播放器以匹配摄影机的地方,您应该放置此代码(必要时替换名称)这是一张它的图片Visual Studio代码:啊,糟糕,我意外地放置了一个:应该有一个?,
float rotateSpeed=Mathf.Abs(angleDelta)>turnstresholdDegrees?数学符号(角度增量)*播放速度:0float rotateSpeed=0的简写;如果(数学绝对值(角度增量)>旋转阈值度)旋转速度=数学符号(角度增量)
float turnThresholdDegrees = 15;
float playerTurnSpeed = 1;

// Get delta angle in degrees on the Y axis between the direction player is facing and the direction camera is facing
float angleDelta = Vector3.SignedAngle(player.transform.forward, camera.transform.forward, Vector3.up);

// Calculate a rotation speed, clamping it to 0 when the angle delta is < some threshold
float rotateSpeed = Mathf.Abs(angleDelta) > turnThresholdDegrees ? Mathf.Sign(angleDelta) * playerTurnSpeed : 0;

// Rotate the player around its local Y axis by the rotation speed
player.transform.Rotate(0, rotateSpeed * Time.deltaTime, 0, Space.Self);