Gravity not applying properly.

so I’m a noob and I’m trying to write a charactercontroller of my own and I had it working before but now gravity isn’t applying properly.

The problem is that gravity does seem to be applying, the player is spawns in the air, but he falls to the ground in super choppy way and when i use my jump function I fly up into the air and get stuck.

could anyone take a look for me?

if you happen to see anything i could be doing better (which may be most of it lol!)

public enum states { Idle, Walking, Strafing, Running, Jumping, Attacking, Talking }
public enum directions { Front, Back, Left, Right }

public var playerState = states.Idle;
public var playerDirection = directions.Front;
var forwardUI : GameObject;

var speed : float = 7;
var runSpeed : float = 13;
var jumpSpeed : float = 15.0;
var turnSpeed : float = 60;
var gravity : float = 20.0;
var turnLock = true;
var theBullet : Transform;
var bulletSpawner : Transform;
var lookAtTarg : Transform;

private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;
var playerBody : GameObject;

function Start(){
    controller = GetComponent(CharacterController);
    var horizontalVelocity : Vector3 = controller.velocity;
	var horizontalSpeed : float = horizontalVelocity.magnitude;
}

function Update() {
    horizontalVelocity = controller.velocity;
    horizontalSpeed = horizontalVelocity.magnitude;

	// Handles the input for the run button
	if(Input.GetButtonDown("run")){
		turnLock = false;
	}
	if(Input.GetButtonUp("run")){
		turnLock = true;
	}
	
	if(turnLock == true  playerDirection != directions.Back)
	{
		playerDirection = directions.Back;
	}
		//Snap Camera Code goes here
		//if(playerDirection == directions.Back){
		//    transform.Rotate(0, 180, 0);
		//    playerDirection = directions.Front;
		//}else if(playerDirection == directions.Right){
		//	transform.Rotate(0, 90, 0);
		//	playerDirection = directions.Front;
		//}else if(playerDirection == directions.Left){
		//	transform.Rotate(0, -90, 0);
		//	playerDirection = directions.Front;
		//}else if(playerDirection == directions.Front){
		//}

	if(Input.GetButtonDown("fire")){
		fireWeapon();
	}

	if(Input.GetAxis("Mouse X") != 0){
	   	var turn: float = Input.GetAxis("Mouse X");
    	transform.Rotate(0, turn * 16 * Time.deltaTime, 0);
    }
        	
    if(Input.GetAxis("Vertical") != 0  controller.isGrounded  Input.GetAxis("Horizontal") == 0){
    		//Forward/Backward Movement
    		moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    		
    		if(Input.GetAxis("Vertical") > 0){
    			//Forward movement, no need to check on turnlock
    			if(playerDirection != directions.Back){
    				playerDirection = directions.Back;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		    if(Input.GetAxis("Vertical") < 0){
    			//Backward movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Front  turnLock == false){
    				playerDirection = directions.Front;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		//print("Forward/Backward movement detected");
    	}
    else if(Input.GetAxis("Horizontal") != 0  controller.isGrounded  Input.GetAxis("Vertical") == 0){
    		//Left/Right
    		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
    		
    		if(Input.GetAxis("Horizontal") > 0){
    			//Right movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Right  turnLock == false){
    				playerDirection = directions.Right;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		if(Input.GetAxis("Horizontal") < 0){
    			//Left movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Left  turnLock == false){
    				playerDirection = directions.Left;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		//print("Left/Right movement detected");
    	}
    else if(Input.GetAxis("Horizontal") != 0  controller.isGrounded  Input.GetAxis("Vertical") != 0){
    		moveDirection = Vector3(Input.GetAxis("Horizontal") / 1.5, 0, Input.GetAxis("Vertical") / 1.5);
    		//print("Diag movement detected");
    		
    		//Placed this code here to grant forward and backward directions priorit in a diagonal situation
    		if(Input.GetAxis("Vertical") > 0){
    			//Forward movement, no need to check on turnlock
    			if(playerDirection != directions.Back){
    				playerDirection = directions.Back;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		    if(Input.GetAxis("Vertical") < 0){
    			//Backward movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Front  turnLock == false){
    				playerDirection = directions.Front;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    }
    else if(Input.GetAxis("Horizontal") == 0  Input.GetAxis("Vertical") == 0  controller.isGrounded){
    		moveDirection = Vector3(0, 0, 0);
    		if(playerState != states.Idle){
    			playerState = states.Idle;
    		}
    		//print("no movement detected");
    }
    
    if(!controller.isGrounded)
    {
    		//print("character in air");
    		playerState = states.Jumping;
    }
    
    if (Input.GetButton ("Jump")  controller.isGrounded) {
        moveDirection.y = jumpSpeed;
    }

    moveDirection = transform.TransformDirection(moveDirection);
    

    
    if(turnLock == true){
   		moveDirection *= speed;
    }else if(turnLock == false){
    	moveDirection *= runSpeed;   
    }
    // Apply gravity
    moveDirection.y -= 15 * Time.deltaTime;	
    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);

    //print(horizontalSpeed);
    
    //print(playerDirection);
}

function fireWeapon(){
	var newBullet = Instantiate(theBullet, bulletSpawner.position, bulletSpawner.rotation);
	Destroy(newBullet.gameObject, 2);
}

Wow, I am not getting what half that code does. Seems a bit extreme, but it it works for what you want.

Attached is a piece of older code that I did that will contain the gravity part you need.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
	// generic controls
	float speed = 8;
	float jumpSpeed = 12;
	
	// character controller
	CharacterController controller;
	public Vector3 move;
	public bool grounded = false;
	
	// camera control
	float x=0;
	float y=0;
	
	// Use this for initialization
	void Start () {
		controller = GetComponent<CharacterController>();
		if(!controller)
			controller = gameObject.AddComponent<CharacterController>();
		
		Vector3 angles = Camera.main.transform.eulerAngles;
		x = angles.y;
		y = angles.x;
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
		input = transform.TransformDirection(input);
		if(input.magnitude > 1) input.Normalize();
		float gravity = Physics.gravity.y * 3;
		
		if(grounded){
			move = input * speed;
			move.y = gravity * Time.deltaTime;
			
			if(Input.GetButtonDown("Jump")) move.y = jumpSpeed;
		} else {
			move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
			move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
			move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
		}
		
		transform.rotation = Camera.main.transform.rotation;
		Vector3 euler = transform.eulerAngles;
		euler.x = 0;
		transform.eulerAngles = euler;
		
		CollisionFlags flags = controller.Move(move * Time.deltaTime);
		grounded = (flags  CollisionFlags.Below) > 0;
	}
	
	void LateUpdate(){
		// quick mouse orbit
		x += Input.GetAxis("Mouse X") * 5;
		y -= Input.GetAxis("Mouse Y") * 5;
		
		if(y > 180) y -= 360;
		if(y < -180) y += 360;
		y = Mathf.Clamp(y, -60,60);
		
		Camera.main.transform.rotation = Quaternion.Euler(y,x,0);
		Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position;
	}
}

See this is what I don’t get, line 71 is where my gravity is coming in and for whatever it simply isn’t

when I compare my code to yours I see that where gravity comes in we are on basically the same page here so what in the world in my mess of code could possibly be interfering with what I’m doing?

I did notice though that it looks like you’re applying gravity a certain way on 38 then 43 one, on isgrounded and one when not grounded.

Maybe this could be my problem…

i’ll take another look

if you want to see what the end result of my messes of code has been you can take a peek at http://pasdu.panickmedia.com/WebPlayer/ … It’ll ask for a user/pass which is just web/web

the controls i have on the website are a lie (they are from the controller that used to work properly! WASD to move, mouse to aim, ctrl to shoot and hold shift to run (tab selects a menu option)

I’m going to see if I can’t maybe get gravity to pay a littlemore attention when the player isn’t grounded!

I just traced my moveDirection.y and after I jump it becomes NaN after I get stuck in the roof.

I’m making progress, it turns out that checking if the character was grounded in my if statments in order to determine the character’s direction of movement and all of that were screwing up the jump somehow, now my only problem is that when the character jumps he kinda juts up in the air and falls back to the ground, I’ve re pasted my code.

It’s looking like he teleports into the air depending on how high i set my jump speed, although he does fall nicely back down to the ground.

I also updated the web player that I posted a link to earlier to reflect my change

public enum states { Idle, Walking, Strafing, Running, Jumping, Attacking, Talking }
public enum directions { Front, Back, Left, Right }

public var playerState = states.Idle;
public var playerDirection = directions.Front;
var forwardUI : GameObject;

var speed : float = 7;
var runSpeed : float = 13;
var jumpSpeed : float = 15.0;
var turnSpeed : float = 60;
var gravity : float = 20.0;
var turnLock = true;
var theBullet : Transform;
var bulletSpawner : Transform;
var lookAtTarg : Transform;

private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;
var playerBody : GameObject;

function Start(){
    controller = GetComponent(CharacterController);
    var horizontalVelocity : Vector3 = controller.velocity;
	var horizontalSpeed : float = horizontalVelocity.magnitude;
}

function Update() {
    horizontalVelocity = controller.velocity;
    horizontalSpeed = horizontalVelocity.magnitude;

	// Handles the input for the run button
	if(Input.GetButtonDown("run")){
		turnLock = false;
	}
	if(Input.GetButtonUp("run")){
		turnLock = true;
	}
	
	if(turnLock == true  playerDirection != directions.Back)
	{
		playerDirection = directions.Back;
	}
		//Snap Camera Code goes here
		//if(playerDirection == directions.Back){
		//    transform.Rotate(0, 180, 0);
		//    playerDirection = directions.Front;
		//}else if(playerDirection == directions.Right){
		//	transform.Rotate(0, 90, 0);
		//	playerDirection = directions.Front;
		//}else if(playerDirection == directions.Left){
		//	transform.Rotate(0, -90, 0);
		//	playerDirection = directions.Front;
		//}else if(playerDirection == directions.Front){
		//}

	if(Input.GetButtonDown("fire")){
		fireWeapon();
	}

	if(Input.GetAxis("Mouse X") != 0){
	   	var turn: float = Input.GetAxis("Mouse X");
    	transform.Rotate(0, turn * 16 * Time.deltaTime, 0);
    }
        	
    if(Input.GetAxis("Vertical") != 0  Input.GetAxis("Horizontal") == 0){
    		//Forward/Backward Movement
    		moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    		
    		if(Input.GetAxis("Vertical") > 0){
    			//Forward movement, no need to check on turnlock
    			if(playerDirection != directions.Back){
    				playerDirection = directions.Back;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		    if(Input.GetAxis("Vertical") < 0){
    			//Backward movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Front  turnLock == false){
    				playerDirection = directions.Front;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		//print("Forward/Backward movement detected");
    	}
    else if(Input.GetAxis("Horizontal") != 0  Input.GetAxis("Vertical") == 0){
    		//Left/Right
    		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
    		
    		if(Input.GetAxis("Horizontal") > 0){
    			//Right movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Right  turnLock == false){
    				playerDirection = directions.Right;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		if(Input.GetAxis("Horizontal") < 0){
    			//Left movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Left  turnLock == false){
    				playerDirection = directions.Left;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		//print("Left/Right movement detected");
    	}
    else if(Input.GetAxis("Horizontal") != 0  Input.GetAxis("Vertical") != 0){
    		moveDirection = Vector3(Input.GetAxis("Horizontal") / 1.5, 0, Input.GetAxis("Vertical") / 1.5);
    		//print("Diag movement detected");
    		
    		//Placed this code here to grant forward and backward directions priorit in a diagonal situation
    		if(Input.GetAxis("Vertical") > 0){
    			//Forward movement, no need to check on turnlock
    			if(playerDirection != directions.Back){
    				playerDirection = directions.Back;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    		
    		    if(Input.GetAxis("Vertical") < 0){
    			//Backward movement, we need to see if we're locked forwards
    			if(playerDirection != directions.Front  turnLock == false){
    				playerDirection = directions.Front;
    			}
    			if(horizontalSpeed > 0  playerState != states.Walking){
    				playerState = states.Walking;
    			}
    		}
    }
    else if(Input.GetAxis("Horizontal") == 0  Input.GetAxis("Vertical") == 0){
    		moveDirection = Vector3(0, 0, 0);
    		if(playerState != states.Idle){
    			playerState = states.Idle;
    		}
    		//print("no movement detected");
    }
    
    if(!controller.isGrounded)
    {
    		//print("character in air");
    		playerState = states.Jumping;
    }
    
    //apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
    
    if (Input.GetButtonDown("Jump")  controller.isGrounded) {
        moveDirection.y = jumpSpeed;
        print("Jump Cmd sent");
    }

    moveDirection = transform.TransformDirection(moveDirection);
    
    if(turnLock == true){
   		moveDirection *= speed;
    }else if(turnLock == false){
    	moveDirection *= runSpeed;   
    }
	
    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
	//print(moveDirection.y);
    //print(horizontalSpeed);
    
    //print(playerDirection);
}

function fireWeapon(){
	var newBullet = Instantiate(theBullet, bulletSpawner.position, bulletSpawner.rotation);
	Destroy(newBullet.gameObject, 2);
}

Edit: okay, after a lot of hairpulling I was able to get this one figured out, my jump isn’t the greatest, but i did add the whole mechanic of holding jump to jump higher, so thanks for the help I’ve got myself a hack!