!!!URGENT need help with gravity UGENT!!!

Ok Im making a project and I cant seem to make my character to go DOWN when i jump he either starts flying or gets stuck to ground. my script as is is:

var character: CharacterController;
var speed: float = 5.0;   // moving speed
var direction: float = 1; // direction (-1 means -x)
var directionup: float = 2;
var directionback: float = -1;
var runanimation: AnimationClip;
var placeanimation: AnimationClip;
var attackanimation: AnimationClip;
var dieanimation: AnimationClip;
var idleanimation: AnimationClip;
var jumpanimation: AnimationClip;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;

function Start(){	
    	character = transform.GetComponent(CharacterController);
}

function Update(){
	if(Input.GetKey("d")){
    	character.Move(Vector3.forward * speed * direction * Time.deltaTime);
    		animation.Play(runanimation.name);
    	}
    if(Input.GetKey("a")){
    	character.Move(Vector3.forward * speed * directionback * Time.deltaTime);
    		animation.Play(runanimation.name);
    	}
    if(Input.GetKey("w")){
    	character.Move(Vector3.right * speed * directionback * Time.deltaTime);
    	}
    if(Input.GetKey("s")){
    	character.Move(Vector3.right * speed * direction * Time.deltaTime);
    	}
   	if(Input.GetMouseButton(1)){
   		animation.Play(placeanimation.name);
   				animation.Stop(idleanimation.name);
   	}
   	if(Input.GetKey("space")){
   		character.Move(Vector3.up * speed * direction * Time.deltaTime);
   			animation.Play(jumpanimation.name);
   	}
   	if (Mathf.Abs(Input.GetAxis("Vertical")) <= 0.1){
        animation.CrossFade(idleanimation.name);
    }
    if(Input.GetMouseButton(0)){
    		animation.Play(attackanimation.name);
    			animation["attack"].layer = -1;
    				animation.Stop(idleanimation.name);
    }
}
function LateUpdate(){
	 	moveDirection.y -= gravity * Time.deltaTime;
			character.Move(moveDirection * Time.deltaTime);
}
its urgent that i get some help soon Thank you everyone

@Tracey I'm not sure I understand what's going on, and hope that you can give a bit more specific information. You say 'when the character jumps' - I assume that refers to Input.GetKey("space")? You say the character either starts flying or gets stuck to the ground - can you explain a little more what you mean by 'flying' (does the character just continue to rise at a steady pace, or hover, or...), and 'stuck to the ground?' What about other components attached to the character? Any other scripts, rigidbody, character controllers, etc.?

I give you -1 for using URGENT in your subject/title. NO question is more important than anyone elses in here. This forum is about great answers for common problems. If you are so important/urgent, hire on of us to solve it through the normal forum. Thanks.

Dude I said urgent because this is for a college project that was due the next dday

1 Answer

1

You almost made it! The trick is to make all vertical movement using moveDirection.y. If the character is grounded, zero it. To jump, set y to an appropriate velocity - but only while it’s grounded, or else your character will go up like a rocket for the eternity!

You just need to change the “if (jump)” to get the gravity/jump effects running:

...
function Update(){
    ...
    // modify the if (Input.getKey("space")) to this:
    if (character.isGrounded){ // only check jump if grounded!
        moveDirection.y = 0; // zero vert speed if grounded
        if (Input.GetKey("space")){ // if jump pressed...
            moveDirection.y = jumpSpeed; // set vert speed to jumpSpeed
            animation.Play(jumpanimation.name); // start your jump animation
        }
    }
    ...

You probably also want to change GetKey to GetKeyDown or GetKeyUp, so it won't fire every frame that the space key is held down; rather, it will fire once when the user either presses or releases space. Good luck.

I tryed that and when i did it didnt play the animation all the way

Have you solved the animation problem?

No not yet :C

I'm stupid about animations, but I read somewhere in the docs that you may have "weights" assigned to each animation: if jump has a weight higher than the other animations, it will play even if other animation has been started.