Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Planetational Gravity First Person Controller Glitch/Bug

Discussion in 'Physics' started by Rugbug_Redfern, Jun 2, 2017.

  1. Rugbug_Redfern

    Rugbug_Redfern

    Joined:
    Jun 2, 2017
    Posts:
    20
    I have a planet, which is just a default sphere GameObject resized to have a radius of 25 units. It has a script called Gravity_Attractor attached to it. My player is a capsule, with a rigidbody and a capsule collider. There are 2 scripts attached to the player; Gravity_Body and FirstPersonController.

    My code for Gravity_Attractor.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gravity_Attractor : MonoBehaviour {
    6.  
    7.     public float gravity = -9.8f;
    8.  
    9.     public void Attract(Rigidbody body) {
    10.         Vector3 targetDir = (body.position - transform.position).normalized;
    11.         Vector3 bodyUp = body.transform.up;
    12.  
    13.         body.rotation *= Quaternion.FromToRotation(bodyUp, targetDir);
    14.         body.AddForce(targetDir * gravity);
    15.     }
    16. }
    17.  
    My code for Gravity_Body.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent (typeof(Rigidbody))]
    6. public class Gravity_Body : MonoBehaviour {
    7.  
    8.     private Gravity_Attractor planet;
    9.     private Rigidbody rb;
    10.  
    11.     void Awake() {
    12.         rb = GetComponent<Rigidbody>();
    13.         planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<Gravity_Attractor>();
    14.  
    15.         rb.useGravity = false;
    16.         rb.constraints = RigidbodyConstraints.FreezeRotation;
    17.     }
    18.  
    19.     void FixedUpdate() {
    20.         planet.Attract(rb);
    21.     }
    22. }
    23.  
    My code for FirstPersonController.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FirstPersonController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.  
    9.     public float mouseSensitivityX;
    10.     public float mouseSensitivityY;
    11.     public float walkSpeed;
    12.     public float runSpeed;
    13.     public float smoothTime;
    14.  
    15.     public GameObject playerCamera;
    16.     private float verticalLookRotation;
    17.  
    18.     private Vector3 moveAmount;
    19.     private Vector3 smoothMoveVelocity;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         rb = GetComponent<Rigidbody>();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.         transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivityX);
    29.         verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivityY;
    30.         verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60f, 60f);
    31.         playerCamera.transform.localEulerAngles = Vector3.left * verticalLookRotation;
    32.  
    33.         Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
    34.         Vector3 targetMoveAmount = moveDir * walkSpeed;
    35.         moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, smoothTime);
    36.     }
    37.  
    38.     void FixedUpdate() {
    39.         rb.MovePosition(rb.position + (transform.TransformDirection(moveAmount) * Time.fixedDeltaTime));
    40.     }
    41. }
    42.  
    I've been following the tutorial by Sebastian Lague (Click here for the tutorial).

    My problem is that occasionaly, when walking around the planet, the player capsule will completely flip out and rotate in random directions (just spazzing around), making it impossible to play. This usually happens when not moving according to the world axis, and instead around a local axis of the planet. Does anyone have an idea of why? Thanks!
     
    Last edited: Jun 6, 2017
  2. Rugbug_Redfern

    Rugbug_Redfern

    Joined:
    Jun 2, 2017
    Posts:
    20
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    It's different because Quaternion multiplication is not commutative.
     
    Rugbug_Redfern likes this.