C# 统一总是使目标前进
我可以移动和旋转我的相机。“我的代码”的旋转通过旋转相机的父对象来实现,而“我的运动代码”也会移动父对象 但是,当我旋转父对象时,我的移动代码出错。目前,如果我用两个手指向上滑动,我希望我的相机总是根据它所看的位置向前移动。当我向下、向左和向右移动时,情况类似。我想让我的相机分别向后、向左和向右移动 目前,当我第一次启动我的应用程序时,一切正常。但当我向右旋转父对象90度时,当我向上滑动时,我的相机会向左移动。因为我不确定如何从我的前进方向抵消这个旋转 以下是我的两种方法:C# 统一总是使目标前进,c#,user-interface,unity3d,touch,C#,User Interface,Unity3d,Touch,我可以移动和旋转我的相机。“我的代码”的旋转通过旋转相机的父对象来实现,而“我的运动代码”也会移动父对象 但是,当我旋转父对象时,我的移动代码出错。目前,如果我用两个手指向上滑动,我希望我的相机总是根据它所看的位置向前移动。当我向下、向左和向右移动时,情况类似。我想让我的相机分别向后、向左和向右移动 目前,当我第一次启动我的应用程序时,一切正常。但当我向右旋转父对象90度时,当我向上滑动时,我的相机会向左移动。因为我不确定如何从我的前进方向抵消这个旋转 以下是我的两种方法: void MoveC
void MoveCamera ()
{
Vector2 delta = Input.GetTouch(0).deltaPosition;
// invert option is if I want to manually change the direction everything moves
// move fingers left to go right etc
float positonX = delta.x * sensitivityX * Time.deltaTime;
positonX = invertX ? positonX : positonX * -1;
float positionY = delta.y * sensitivityY * Time.deltaTime;
positionY = invertY ? positionY : positionY * -1;
transform.parent.position += new Vector3(positonX, 0, positionY);
}
Vector2 CameraOrbit ()
{
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
//vertical movement is corresponded to rotation in x-axis
orbitSpeedX = -touchDeltaPosition.y * rotXSpeedModifier;
//horizontal movement is corresponded to rotation in y-axis
orbitSpeedY = touchDeltaPosition.x * rotYSpeedModifier;
//get the current rotation
float x = transform.parent.rotation.eulerAngles.x;
float y = transform.parent.rotation.eulerAngles.y;
//make sure x is between -180 to 180 so we can clamp it propery later
if (x > 180)
{
x -= 360;
}
//y = ClampAngle(y, yMinLimit, yMaxLimit);
//calculate the x and y rotation
Quaternion rotationY = Quaternion.Euler (0, y, 0) * Quaternion.Euler (0, orbitSpeedY, 0);
Quaternion rotationX = Quaternion.Euler (Mathf.Clamp (x + orbitSpeedX, minRotX, maxRotX), 0, 0);
//apply the rotation
transform.parent.rotation = rotationY * rotationX;
orbitSpeedX *= (1 - Time.deltaTime * 12);
orbitSpeedY *= (1 - Time.deltaTime * 3);
return touchDeltaPosition;
}
在我得到这段代码之前,我用transform.Translate移动我的相机,这很有效,我可以向前移动,不管我的旋转如何。但是,当我需要Y位置时,相机Y位置会移动,以保持在我开始移动时的任何高度。这会使它看起来像是缩小了,我越往前推。我想你有一个问题,就是不断朝一个方向移动,而不考虑你所面对的方向 我不确定如何解决您的问题,但以下是我使用的示例: 假设我正在为电脑制作一个第一人称射击游戏。以下是我将用于运动的代码: Vector3 position=transform.position+new Vector3 Input.getAxissome1,Input.getAxissome2,0*transform.direction; transform.position=位置 这将根据方向计算移动。
如果仍然不起作用,请再次评论,我将很乐意提供帮助 你想让它向前发展是什么意思?比如向前走/放大?还是用手指向上滑动?你说出错是什么意思?什么是invertX/invertY?我会编辑我的问题,这样我就不会留下太多的评论。我得到了以下错误运算符*“不能应用于float类型的操作数”和“UnityEngine.Quaternion”@N0xus-Hmm。我会发布我通常使用的大想法,也许你可以尝试在你的程序中使用它一些方法?