- Home /
Faux Gravity, spinning at bottom of sphere
I'm working on a faux style gravity simulation (like mario galaxy) I have it working about 90% but when the player is running around the sphere, after a few rotations they become locked into spinning at the bottom of the sphere I'm completely out of ideas on how to deal with this so any input would be greatly helpful
</p> <pre><code>public float MoveSpeed = 20.0f; public float TurnSpeed = 10.0f; private Vector3 playerGravity = new Vector3(0, -3, 0); private Animation ani; private CharacterController controller; private float Angle = 0.0f; private Quaternion quat = Quaternion.identity; // Use this for initialization void Start () { ani = transform.FindChild("body").animation; controller = transform.GetComponent("CharacterController") as CharacterController; } void UpdateFloorRotation() { GameObject floor = GameObject.FindGameObjectWithTag("Level"); Vector3 floorVec = -transform.position.normalized; playerGravity = floorVec * 0.1f; RaycastHit hitInfo; if (Physics.Raycast(transform.position, floorVec, out hitInfo, 10, 1 << 8)) { Debug.Log("Floor: " + hitInfo.collider.name); transform.rotation = quat * Quaternion.FromToRotation (Vector3.up, transform.position.normalized); Debug.Log("Normal: " + floorVec); } } // Update is called once per frame void Update () { Vector3 Locomotion = Vector3.zero; Vector2 gravity = Physics.gravity; Physics.gravity = playerGravity; if (Input.GetButtonDown("Jump")) { if (ani != null) { //ani.CrossFade("use"); } } if (Input.GetButtonUp("Up") || Input.GetButtonUp("Down") || Input.GetButtonUp("Left") || Input.GetButtonUp("Right")) { if (ani != null) { ani.CrossFade("idle"); } } if (Input.GetButton("Up")) { Locomotion += (-transform.forward * MoveSpeed); if (ani != null) { ani.CrossFade("run"); } } if (Input.GetButton("Down")) { Locomotion += (transform.forward * MoveSpeed); if (ani != null) { ani["walk"].speed = -1; ani.CrossFade("walk"); } } if (Input.GetButton("Left")) { Angle += -TurnSpeed * 1; if (ani != null && !Input.GetButton("Up")) { ani["walk"].speed = 1; ani.CrossFade("walk"); } } if (Input.GetButton("Right")) { Angle += TurnSpeed * 1; if (ani != null && !Input.GetButton("Up")) { ani["walk"].speed = 1; ani.CrossFade("walk"); } } Locomotion *= Time.deltaTime; quat = Quaternion.AngleAxis(Angle, playerGravity.normalized); Vector3 targetDir = transform.TransformDirection(Vector3.down); if (!Physics.Raycast(transform.position, targetDir, 3.3f, 1 << 8)) { Debug.Log("Falling!"); Locomotion += playerGravity; } controller.Move(Locomotion); UpdateFloorRotation(); } </code></pre> <p>
really frustrating because it seems very random, sometimes i can run around the planet 20 times before it happens, others it happens right away, maybe time depended?
Guess it was time related, I changed Update to FixedUpdate and most of the problems went away, now it only does the crazy spinning at the bottom of the sphere when the player has tried to spin there character (Pressed left or right) then it goes hang wire pretty fast.
I figured it out, quat needed to be calculated with Vector3.up, like the rotation in the updateRotation method, then it needs to be the second component multiplied together.
ack, I take that back, its very close now, but now anytime the player is walking around the surface its always moving slightly towards the bottom of the sphere.