- Home /
Character wont jump on moving object
I have two sets of platforms: one set is moving downward and the others are moving upward. The character will not jump on the downward platforms, but will on the upward ones. Im using CharacterController.isGrounded method of collision to be able to jump. My guess is that its not registering that connection when the platform is essentially falling, but the character is clearly standing on it because it will animate the walk while moving back and forth as if it is registering the grounding. Any ideas?
Thanks!
void Update()
{
//Assign static variable
stopControls1 = stopControls;
//Move Player
PlayerRun();
}
//Player control
void PlayerRun()
{
if(!stopControls)
{
//Check if is on the ground
if(charController.isGrounded)
{
//If grounded, set to false
walkJumpOn = false;
runJumpOn = false;
crouchJumpOn = false;
if(!isCrouched)
{
if (!freeFall)
{
//Set key movement
moveDirection = new Vector3 (Input.GetAxis ("LeftAndRight"), 0);
charController.Move (moveDirection * Time.deltaTime);
}
//Rotate
if (moveDirection != Vector2.zero)
{
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
thisTransform.transform.rotation = Quaternion.Slerp(thisTransform.transform.rotation,
newRotation, Time.deltaTime * 20);
}
//If is moving
if(moveDirection.magnitude > 0.05f)
{
//walk
if(!Input.GetKey(KeyCode.M))
{
maxSpeed = 6;
moveDirection *= walkSpeed;
thisTransform.GetComponent<Animation>().CrossFade("Run");
}
//run
else
{
maxSpeed = 10;
moveDirection *= runSpeed;
thisTransform.GetComponent<Animation>().CrossFade("Sprint");
}
//idle
else
{
thisTransform.GetComponent<Animation>().CrossFade("Idle");
}
}
//JUMP COMMANDS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Idle jump height
if(Input.GetButtonDown("Jump") && (!Input.GetKey(KeyCode.M) || Input.GetKey (KeyCode.M)
&& moveDirection.x == 0) && Input.GetAxis("Crouch") >= 0)
{
AudioControl.JumpSound();
CreateDust();
moveDirection.y = walkJump;
moveDirection.x = walkSpeed;
walkJumpOn = true;
}
//Sprinting jump height
if(Input.GetButtonDown("Jump") && Input.GetKey(KeyCode.M) && moveDirection.x != 0)
{
AudioControl.JumpSound();
CreateDust();
moveDirection.y = runJump;
runJumpOn = true;
}
//Walking jump height
if(Input.GetButtonDown("Jump") && moveDirection.x == 0 && Input.GetKey(KeyCode.S))
{
AudioControl.JumpSound();
CreateDust();
moveDirection.y = walkJump;
crouchJumpOn = true;
}
}
//If not on the ground...
if(!charController.isGrounded)
{
//Assign inputs
moveDirection.x = Input.GetAxis("LeftAndRight");
//If jump key is let go, fall to the ground
if(Input.GetButtonUp("Jump"))
{
moveDirection.x = 0;
moveDirection.y = moveDirection.y - fallSpeed;
}
//Create gravity
moveDirection.y -= gravity * Time.deltaTime;
//Check for jump input
charController.Move(moveDirection * Time.deltaTime);
}
}
Answer by CarlHaeggman · Feb 25, 2020 at 09:24 AM
Are you using tags or something like that to identify if the character is standing on an object which it can jump on?
No...Im using CharacterController.isGround. It works fine on all other objects it lands on. And as mentioned, it works fine on the upward moving platforms. Its only the downward ones that dont work.
Answer by ellimyt1 · Feb 25, 2020 at 09:16 PM
Is your character jump mechanic code within the Update or FixedUpdate ? The latter is for physics, so you probably want all movement stuff within Update itself. This should be checked also for your platforms movement code.
Seeing some of your code for these sections would be handy, so that we can reproduce.,@GC1983 is your jump mechanic being checked within the Update() or FixedUpdate()? It probably should be the former. The other change that might be worth doing, is moving where/when your platform movement mechanic happens.
Would be helpful to see some code with these two sections, so that we can reproduce.
I appreciate your response. I added the script. I tried used FixedUpdate() and it made no difference. I found a video on jumping mech from Press Start. Ill try that out and come back with results.
Your answer
Follow this Question
Related Questions
How does character controller's isGrounded() detect whether the character's on the ground? 1 Answer
CharacterController.isGrounded not working after Unity 5 4 Answers
"Object reference not set to an instance of an object" error 2 Answers
How do simple moving platform? 1 Answer
problem with moving platform 1 Answer