# Help with gravity/player rotation?

Hey,

I’m currently in the process of trying to change the direction of the gravity, and rotate the player as it falls but I’m having some problems.

Here is an example of what I am trying to achieve

The problems I am having are it’s not rotating properly, it’s quite buggy for instance if you go from a 90 degree angle it messes up. Second is when the player lands im trying to get it so it lands perfectly and there arent any physics making it wobble as it lands. (Cant think of the correct term its escaped me!)

Here is what I have so far, attached to a cube which has a child camera at 0,0,0!

``````public float gravity = 300.0F;	// HOW MUCH GRAVITY IS APPLIED?
public int _dirGravity = 0;		// WHICH WAY IS GRAVITY FACING?
public float speedRotation = 0.3F;

void Update() {

switch(_dirGravity) {
case 0:  // DOWN
gravity = -500;
Physics.gravity = new Vector3(0.0F,gravity * Time.deltaTime,0.0F);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0F,0.0F,0.0F), Time.deltaTime * speedRotation);
break;
case 1:  // UP
gravity = 500;
Physics.gravity = new Vector3(0.0F,gravity * Time.deltaTime,0.0F);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0F,0.0F,180.0F), Time.deltaTime * speedRotation);
break;
case 2:  // LEFT
gravity = -500;
Physics.gravity = new Vector3(gravity * Time.deltaTime,0.0F,0.0F);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0F,0.0F,270.0F), Time.deltaTime * speedRotation);
break;
case 3:  // RIGHT
gravity = 500;
Physics.gravity = new Vector3(gravity * Time.deltaTime,0.0F,0.0F);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0.0F,0.0F,90.0F), Time.deltaTime * speedRotation);
break;
case 4:  // BACKWARD
gravity = -500;
Physics.gravity = new Vector3(0.0F,0.0F,gravity * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(90.0F,0.0F,0.0F), Time.deltaTime * speedRotation);
break;
case 5:  // FORWARD
gravity = 500;
Physics.gravity = new Vector3(0.0F,0.0F,gravity * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(270.0F,0.0F,0.0F), Time.deltaTime * speedRotation);
break;
}
}
``````

The other thing I’m trying to achieve is the mouse look but I really can’t figure this out for the life of me, I’ve tried adding the generic mouselook script and altering its properties for each gravity direction but I can’t get it to work smoothly.

Any ideas, theories or help is appreciated.

Thanks,

Carl.

Well, I don’t know how to help you with the gravity aspect of your game, but here is a script that you could use for the mouse look problem:

``````function Update(){

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
var forward_ = transform.TransformDirection(Vector3.forward);

if (Physics.Raycast (ray, hit, 300)) {
var destPoint = hit.point;
var target = Vector3(destPoint.x, destPoint.y, destPoint.z);
transform.LookAt(target);
}

Debug.DrawRay(transform.position, forward_ * 300, Color.red);

}
``````

I will continue to think about the gravity problem though. Good luck with it!

–CgRobot–