# Move camera forward regardless of rotation

I have the ability to both move and rotate my camera. The rotation of my code works by rotating the parent object of my camera and my movement code also moves my parent object.

However, when I rotate the parent object, my movement code goes awry. At the moment, if I use two fingers to and swipe up, I want my camera to always move forward. Similar for when I move down, left and right.

These are my two methods:

``````  void MoveCamera ()
{

Vector2 delta = Input.GetTouch(0).deltaPosition;

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);

}
``````

I have a feeling itâ€™s a single line Iâ€™m missing but how I can I change my code so that no matter what my rotation is, I always move forward when I move my fingers up a screen?

Iâ€™ve been looking around, and I know I need to pass in the camera Vector3.forward into the movement method. However, I need the X and Y position that I get from my finger touches and pass into the movement. Does anyone know of a way i can combine these?

Perhaps you should change it so that rotation and movement are handled by different parent objects. For example:

• Parent 1: movement
â€“ Parent 2: rotation
â€” Camera

This way you can move the camera all around, and the rotation stays the same. Likewise, you can rotate with affecting movement.

as @blizzy says, youâ€™re not using a basic point and shoot camera, youâ€™ll need to have a camera â€śrigâ€ťâ€¦ in filming youâ€™d have the camera dolly (direction movement), on that you have the camera mount (rotation) and then the camera.

1 Like

Been at this for days and that never occured to me. However, Iâ€™ve just tried that and the issue is still the same.

If I move the first, and rotate the second parent, say 180 degrees, then why I swipe up, my first parents movement is reversed and moves down as opposed to forward.

It shouldnâ€™t because it never rotates.

``````    void MoveCamera ()
{

Vector2 delta = Input.GetTouch(0).deltaPosition;

float positonX = delta.x * sensitivityX * Time.deltaTime;
positonX = invertX ? positonX : positonX * -1;

float positionY = delta.y * sensitivityY * Time.deltaTime;
positionY = invertY ? positionY : positionY * -1;

cameraMove.transform.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 = cameraRotate.transform.rotation.eulerAngles.x;
float y = cameraRotate.transform.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
cameraRotate.transform.rotation = rotationY * rotationX;
orbitSpeedX *= (1 - Time.deltaTime * 12);
orbitSpeedY *= (1 - Time.deltaTime * 3);