# Rotating for gravity.

Hello,

I have created a pickup that allows the player to change the direction of the gravity (can change to x, y, z axis).

However, to be able to fully move in the modified gravity direction, I would need to rotate the player. The player object has 3 childs, the marble, the button collider, and the main camera.

I have tried with transform.rotate, however, it isn’t rotating the viewport of the camera. only the other 2 object. How shoul I do it?

Also, because you can change the gravity to 6 directions (x, -x, y, -y, z, -z), how should I calculate how much rotation is needed for each variation (somekind of a script that do tha calculation, because programming all the variation manually may be not the best solution)?

Thanks,
Akos

If the camera is a player’s child, it should rotate or move with the player, no doubt about this. It would not do it only if some script attached to the camera was controlling its orientation (a child always follows the parent movement and rotation, but the parent isn’t affected by the child).

The best way to set the player orientation according to gravity is to set its transform.up vector to the negative of the gravity, like this:

```  transform.up = -myGravity.normalized;
```

where transform is the player’s transform, of course, and myGravity is a vector pointing to the down direction in your system (like Physics.gravity in a regular system).

EDITED2: This version of MouseOrbit keeps the parent object orientation, but follows the target position. Save this with a different name (MouseOrbitChild.js, for instance) and assign it to the camera (remove the original MouseOrbit).

```// follow the target with mouse X and Y control
// but keeping the parent object orientation
var target: Transform;
var distance = 10.0; // distance from target
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

function Start () {
var angles = transform.localEulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody) rigidbody.freezeRotation = true;
}

function LateUpdate () {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
transform.localRotation = rotation;
if (target){
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.position = position;
}
}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360) angle += 360;
if (angle > 360) angle -= 360;
return Mathf.Clamp (angle, min, max);
}
```

EDITED3: One more version! This time the script makes the camera follow the target object, but with orientation based on Physics.gravity. In theory, it doesn’t matter if the camera is childed to some object or not, so you can remove the camera from the parent object if you want.

```var target: Transform;
var distance = 10.0; // distance from target
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = 0;
var yMaxLimit = 60;

private var x = 0.0;
private var y = 0.0;

function Start () {
// Make the camera rigidbody (if any) ignore physics rotation
if (rigidbody) rigidbody.freezeRotation = true;
}

function LateUpdate () {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
// apply gravity orientation
var rotation = Quaternion.FromToRotation(Vector3.up, -Physics.gravity);
rotation *= Quaternion.Euler(y, x, 0); // apply mouse control
transform.rotation = rotation; // set camera orientation
if (target){
var position = rotation * Vector3(0,1,-distance) + target.position;
transform.position = position;
}
}
```