Playing with the Character Motor Gravity PROBLEM

Hi there!

I’m developing a 2.5D platformer game where you can play with the Gravity, when you press SPACE you change the Gravity direction (up or down) and it works well, BUT just if I’m jumping while pressing SPACE, if I press SPACE and then I try to move, my character can’t move anymore and the console gives me the following error:

transform.positionWithLocalOffset assign attempt for ‘PlayerHitbox’ is not valid. Input positionWithLocalOffset is { NaN, NaN, NaN }.
UnityEngine.CharacterController:Move(Vector3)
CharacterMotor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.cs:249)
CharacterMotor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.cs:367)

Here is my script for change the gravity:

    using UnityEngine;
    using System.Collections;
    
    public class ChangeGravity : MonoBehaviour
    {
        public bool gravityInverted;
        public float gravityInvertedCount;
        public float playerGravity; //NOT USED AT THE MOMENT
        public CharacterMotor charMotor;
        // Use this for initialization
        void Start()
        {
            charMotor = GetComponent<CharacterMotor>();
            gravityInverted = false;
        }
    
        // Update is called once per frame
        void Update() {
    
            if(Input.GetKeyDown ("space"))
            {
                gravityInvertedCount++;
                gravityInverted = true;
            }
    
            if (Input.GetKeyDown("down"))
            {
    
                gravityInvertedCount = 2;
            }
    
            if (gravityInverted == true)
            {
    
                charMotor.movement.gravity = -9.8f;
            }
    
            if (gravityInverted == false)
            {
    
                charMotor.movement.gravity = 9.8f;
            }
    
            if (gravityInvertedCount >= 2)
            {
                print("Gravity Inverted Count equals 0 again");
                gravityInvertedCount = 0;
                gravityInverted = false;
            }
    
         
    	}
    
    
    
    }

Anyone understands what is happening?

i think you should post your movement script too.
Also, why not shorten your script to:

bool gravityInverted = false; //maybe you need it for something else too
if(Input.GetKeyDown ("space")) {
    charMotor.movement.gravity =-9.8f;
    gravityInverted = true;
}
if(Input.GetKeyDown("down") {
    charMotor.movement.gravity =9.8f;
    gravityInverted = false;
}

?

I’m using the Unity’s default platformer movement script so I thought you should know what script is and why is happening this error.

Someone told me to write my own movement script, maybe I’ll do that.

Posting in case someone stumbles upon the same problem as there are apparently at least two or three people who have had this trouble. The reason for the error is that changing gravity to negative value causes CalculateJumpVerticalSpeed function to return NaN due to square root being taken from a multiple of gravity and this value travels around until it ends up being passed to Move function that throws an exception.

To FIX change CalculateJumpVerticalSpeed to this (tested with Unity version 4.1.1):

function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
	// From the jump height and gravity we deduce the upwards speed 
	// for the character to reach at the apex.
	return Mathf.Sqrt (2 * targetJumpHeight * Mathf.Abs(movement.gravity));
}

Since I did not want to change the sign of vertical jump speed, I didn’t multiply with sign of gravity afterward taking the square root, but one might have to do that if use case differs.