- Home /
isGrounded not updating
I have a character that starts on a platform and the platform gets destroyed after a couple of seconds. My problem is that when the platform is destroyed the isGrounded is still true. Only after I move the character it gets false and the character finally starts falling. Anyone having the same problem and figured out how to fix this?
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public static Player instance;
CharacterController cc;
float dashSpeed = 10f;
float gravity = 6f;
Transform playerTrans;
float moveTo;
bool right, left = false;
bool blockedLeft, blockedRight = false;
bool allowInput = true;
Ray rightRay, leftRay;
RaycastHit hit;
float rayLength = 1.2f;
void Start ()
{
instance = this;
playerTrans = transform;
moveTo = playerTrans.position.x;
cc = GetComponent<CharacterController> ();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.RightArrow) && allowInput && playerTrans.position.x < 4f && !blockedRight)
MoveRight ();
if(Input.GetKeyDown(KeyCode.LeftArrow) && allowInput && playerTrans.position.x > 0.2f && !blockedLeft)
MoveLeft();
//******Move Right****************//
if (right)
{
if (moveTo > playerTrans.position.x)
{
allowInput = false;
cc.Move (Vector3.right * Time.fixedDeltaTime * dashSpeed);
}
else
{
allowInput = true;
cc.Move (Vector3.zero);
}
}
//*******Move Left***********//
if (left)
{
if (moveTo < playerTrans.position.x)
{
allowInput = false;
cc.Move (Vector3.right * Time.fixedDeltaTime * -dashSpeed);
}
else
{
allowInput = true;
cc.Move (Vector3.zero);
}
}
//**********Raycasting*******************//
if(Physics.Raycast(transform.position,Vector3.left, out hit, rayLength))
{
Debug.Log (hit.collider.tag);
if(hit.collider.tag == "Box")
{
blockedLeft = true;
}
}
else
{
blockedLeft = false;
}
if(Physics.Raycast(transform.position,Vector3.right, out hit, rayLength))
{
Debug.Log (hit.collider.tag);
if(hit.collider.tag == "Box")
{
blockedRight = true;
}
}
else
blockedRight = false;
//*****CHECK IF GROUNDED*********//
if (!cc.isGrounded)
cc.Move(new Vector3(0,-1 * Time.deltaTime * gravity,0));
Debug.Log (cc.isGrounded);
}
//*******Moving Functions***********//
void MoveRight()
{
right = true;
left = false;
moveTo = playerTrans.position.x + 1.0f;
}
void MoveLeft()
{
right = false;
left = true;
moveTo = playerTrans.position.x - 1.0f;
}
//Very ugly work around as the player jumps up slightly, but lower amount on multiplier or using Vector3.zero in some cases still skips updating isGrounded and shows true when it should not.
public void UpdateCC()
{
cc.Move(Vector3.up * 0.2f);
}
}
You need to be more specific about what scripts you are using. If the are non-standard, we would need to see the code that set 'isGrounded'.
Actually EDIT your question, and just put it in there. You still didn't show your code for isGrounded. Just follow http://answers.unity3d.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html
@Fattie: He is obviously using this: https://docs.unity3d.com/Documentation/ScriptReference/CharacterController-isGrounded.html
I'm not exactly an expert when it comes to using the Character Controller as I prefer using Rigidbodies, but I guess calling $$anonymous$$ove with a Vector3.zero whenever a plattform gets destroyed could be a quick (and dirty) workaround for your problem.
Thank you so much pafla for that idea. It now at least works on the starting platform but now ins$$anonymous$$d it gets stuck on the next. How ever destroying another platform after the second one makes it fall again. Tried Vector3.up * 0.2 and it works all the time but now it looks ugly as the character jump every time a platform is destroyed.
Your answer
Follow this Question
Related Questions
bizarre collision modulations with character controller 1 Answer
controller.Move doesn't stay grounded when walking down slope 3 Answers
Problem with CharacterController.isgrounded 1 Answer
Animation through graphics in FPS controller, weird character controller issue, NEED help! 0 Answers
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers