Search Unity

Error when applying force to a rigidbody in planetary gravity simulation

Discussion in 'Scripting' started by papathor, Mar 5, 2015.

  1. papathor

    papathor

    Joined:
    Oct 8, 2012
    Posts:
    41
    Hi,

    I'm implementing a point gravity system (planetary physics). In the script that revolves around an object (a moon, for example) I have:

    private void Vector3 gravityVector;
    void FixedUpdate() {

    if (gravityIgnoresMass) {
    GetComponent<Rigidbody>().AddForce(gravityVector, ForceMode.Acceleration);
    }
    else {
    GetComponent<Rigidbody>().AddForce(gravityVector);
    }

    //clear for next round
    gravityVector = Vector3.zero;
    }


    I'm getting the error: "rigidbody.force assign attempt for 'Sphere' is not valid. Input force is {NaN, NaN, NaN}."

    So something's not right with gravityVector as the import force shows three objects that are not numbers.

    What am I doing wrong and how do I fix it?

    Thanks so much for the help!