我有能力移动和旋转我的相机 . 我的代码的旋转通过旋转我的相机的父对象来工作,我的移动代码也移动了我的父对象 .
但是,当我旋转父对象时,我的移动代码出错了 . 目前,如果我用两根手指向上滑动,我希望我的相机始终根据它的外观向前移动 . 当我向下,向左和向右移动时类似 . 我希望我的相机分别向后,向左和向右移动 .
目前,当我第一次启动我的应用程序时,一切正常 . 但是当我将我的父对象向右旋转90度时,当我向上滑动时,我的相机向左移动 . 因为我不确定如何从我的前进方向抵消这种旋转 .
这是我的两种方法:
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位置会移动,以便在我开始移动时保持在任何高度 . 这会使它看起来像是缩小我推动的前进 .
1 回答
我认为你在不考虑你所面临的方向的情况下,不断朝着一个方向前进?
我不确定如何解决你的问题,但这是我使用的一个例子:
假设我正在为计算机制作第一人称射击游戏 . 这是我将用于运动的代码:
Vector3 position = transform.position new Vector3(Input.getAxis(“some1”),Input.getAxis(“some2”),0)* transform.direction; transform.position = position .
这将根据方向计算运动 . 如果它仍然不起作用,那么再次评论,我将很乐意提供帮助!