Planetational Gravity Glitches

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gravity_Attractor : MonoBehaviour {

    public float gravity = -9.8f;

    public void Attract(Rigidbody body) {
        Vector3 targetDir = (body.position - transform.position).normalized;
        Vector3 bodyUp = body.transform.up;

        body.rotation *= Quaternion.FromToRotation(bodyUp, targetDir);
        body.AddForce(targetDir * gravity);
    }
}

My code for Gravity_Body.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof(Rigidbody))]
public class Gravity_Body : MonoBehaviour {

    private Gravity_Attractor planet;
    private Rigidbody rb;

    void Awake() {
        rb = GetComponent<Rigidbody>();
        planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<Gravity_Attractor>();

        rb.useGravity = false;
        rb.constraints = RigidbodyConstraints.FreezeRotation;
    }

    void FixedUpdate() {
        planet.Attract(rb);
    }
}

My code for FirstPersonController.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstPersonController : MonoBehaviour {

    private Rigidbody rb;

    public float mouseSensitivityX;
    public float mouseSensitivityY;
    public float walkSpeed;
    public float runSpeed;
    public float smoothTime;

    public GameObject playerCamera;
    private float verticalLookRotation;

    private Vector3 moveAmount;
    private Vector3 smoothMoveVelocity;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();
    }
 
    // Update is called once per frame
    void Update () {
        transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivityX);
        verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivityY;
        verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60f, 60f);
        playerCamera.transform.localEulerAngles = Vector3.left * verticalLookRotation;

        Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
        Vector3 targetMoveAmount = moveDir * walkSpeed;
        moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, smoothTime);
    }

    void FixedUpdate() {
        rb.MovePosition(rb.position + (transform.TransformDirection(moveAmount) * Time.fixedDeltaTime));
    }
}

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. Does anyone have an idea of why? Thanks!

Somehow I fixed it - in Gravity_Attractor.cs, I did body.rotation *= Quaternion.FromToRotation(bodyUp, targetDir) when I should have done body.rotation = Quaternion.FromToRotation(bodyUp, targetDir) * body.rotation. Not sure how that’s different, but it works…