C# 防止Y轴在Unity中移动`transform.forward`
我正在Unity中创建一个3D游戏,我有一个脚本,让玩家用鼠标环视四周。为了将玩家移动到他们所看到的方向,我使用transform.forward。我的问题是,当他们看着天花板并按下“W”(前进)时,他们开始上升到空中。基本上,我需要知道是否存在只允许在x轴和z轴上移动的C# 防止Y轴在Unity中移动`transform.forward`,c#,unity3d,3d,unity5,yaxis,C#,Unity3d,3d,Unity5,Yaxis,我正在Unity中创建一个3D游戏,我有一个脚本,让玩家用鼠标环视四周。为了将玩家移动到他们所看到的方向,我使用transform.forward。我的问题是,当他们看着天花板并按下“W”(前进)时,他们开始上升到空中。基本上,我需要知道是否存在只允许在x轴和z轴上移动的transform.forward方法或子方法 这是我的动作脚本(C#): if(transform.rotation.x
transform.forward
方法或子方法
这是我的动作脚本(C#):
if(transform.rotation.x<-10)
{
//不要向前或向后移动
Log(“旋转太大,无法向前移动…”);
tooGoodForMovement=真;
}
其他的
{
Tooodformovement=假;
if(Input.GetKey(KeyCode.W))
{
//前进
player.velocity=(transform.FindChild(“主摄影机”).transform.forward*moveSpeed);
}
if(Input.GetKey(KeyCode.S))
{
//背
player.velocity=(-transform.FindChild(“主摄像机”).transform.forward*moveSpeed);
}
}
if(Input.GetKey(KeyCode.A))
{
//左
player.velocity=-transform.FindChild(“主摄影机”).transform.right*moveSpeed;
}
if(Input.GetKey(KeyCode.D))
{
//对
player.velocity=transform.FindChild(“主摄像机”).transform.right*moveSpeed;
}
尝试将速度向量设置为临时变量,并将Y重置为零
Transform camera;
void Start()
{
//Cache transform.FindChild so that we don't have to do it every time
camera = transform.FindChild("Main Camera");
}
然后在其他函数中:
Vector3 velocity = Vector3.zero;
if (transform.rotation.x < -10)
{
//do no forward or backward movement
Debug.Log("Rotation too great to forward move...");
tooGoodForMovement = true;
}
else
{
tooGoodForMovement = false;
if (Input.GetKey(KeyCode.W))
{
//Forward
velocity = camera.forward * moveSpeed;
}
if (Input.GetKey(KeyCode.S))
{
//Back
velocity = -camera.forward * moveSpeed;
}
}
if (Input.GetKey(KeyCode.A))
{
//Left
velocity = -camera.right * moveSpeed;
}
if (Input.GetKey(KeyCode.D))
{
//Right
velocity = camera.right * moveSpeed;
}
velocity.Y = 0;
player.velocity = velocity;
Vector3速度=Vector3.0;
if(变换旋转x<-10)
{
//不要向前或向后移动
Log(“旋转太大,无法向前移动…”);
tooGoodForMovement=真;
}
其他的
{
Tooodformovement=假;
if(Input.GetKey(KeyCode.W))
{
//前进
速度=摄像机。前进*移动速度;
}
if(Input.GetKey(KeyCode.S))
{
//背
速度=-camera.forward*移动速度;
}
}
if(Input.GetKey(KeyCode.A))
{
//左
速度=-camera.right*移动速度;
}
if(Input.GetKey(KeyCode.D))
{
//对
速度=摄像机。右*移动速度;
}
速度Y=0;
player.velocity=速度;
尝试将速度向量设置为临时变量,并将Y重置为零
Transform camera;
void Start()
{
//Cache transform.FindChild so that we don't have to do it every time
camera = transform.FindChild("Main Camera");
}
然后在其他函数中:
Vector3 velocity = Vector3.zero;
if (transform.rotation.x < -10)
{
//do no forward or backward movement
Debug.Log("Rotation too great to forward move...");
tooGoodForMovement = true;
}
else
{
tooGoodForMovement = false;
if (Input.GetKey(KeyCode.W))
{
//Forward
velocity = camera.forward * moveSpeed;
}
if (Input.GetKey(KeyCode.S))
{
//Back
velocity = -camera.forward * moveSpeed;
}
}
if (Input.GetKey(KeyCode.A))
{
//Left
velocity = -camera.right * moveSpeed;
}
if (Input.GetKey(KeyCode.D))
{
//Right
velocity = camera.right * moveSpeed;
}
velocity.Y = 0;
player.velocity = velocity;
Vector3速度=Vector3.0;
if(变换旋转x<-10)
{
//不要向前或向后移动
Log(“旋转太大,无法向前移动…”);
tooGoodForMovement=真;
}
其他的
{
Tooodformovement=假;
if(Input.GetKey(KeyCode.W))
{
//前进
速度=摄像机。前进*移动速度;
}
if(Input.GetKey(KeyCode.S))
{
//背
速度=-camera.forward*移动速度;
}
}
if(Input.GetKey(KeyCode.A))
{
//左
速度=-camera.right*移动速度;
}
if(Input.GetKey(KeyCode.D))
{
//对
速度=摄像机。右*移动速度;
}
速度Y=0;
player.velocity=速度;
这没问题。请为FindChild(“主摄像头”)
添加每次都已完成的修复。如果能抓住这个机会就好了,这很有效。非常感谢。我早该想到的。还有@Programmer,修复FindChild(“主摄像头”)
是什么意思?@Sub6Resources检查更新的答案。一直使用FindChild
代价高昂。缓存转换。@程序员感谢更新的答案。是的,我应该这么做。我会马上修好的,没关系。请为FindChild(“主摄像头”)
添加每次都已完成的修复。如果能抓住这个机会就好了,这很有效。非常感谢。我早该想到的。还有@Programmer,修复FindChild(“主摄像头”)
是什么意思?@Sub6Resources检查更新的答案。一直使用FindChild
代价高昂。缓存转换。@程序员感谢更新的答案。是的,我应该这么做。我会马上修好的。