- Home /
Making the player fall if it hits the ceiling
Hi!
I am making a player controller and I am using the Character Controller and the code that can be found in the Unity Scripting API. I am still learning how to use this and I need some help. Whenever my player hits the ceiling you can kinda float around there for a little while until you fall down again. How do I make so the player will fall when they hit the ceiling and not just float for a bit before falling?
Here is my code:
moveDirection.y -= playerStats.gravity * Time.deltaTime;
cc.Move(moveDirection * Time.deltaTime);
if(cc.isGrounded){
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= playerStats.speed;
if(Input.GetButtonDown ("Jump")){
moveDirection.y = playerStats.jumpHeight;
}
}
Thanks in advance
Answer by JustFun · Aug 11, 2014 at 06:58 PM
Ok, add this to your controller script:
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.collider.tag == "Ceiling")
{
moveDirection.y = -moveDirection.y;
}
}
And do not forget to assign tag "Ceiling" to your ceiling surfaces.
If you want it to work on complex Mesh Colliders or if you can't change the ceiling tag for whatever reason, you may want to use:
if (hit.moveDirection.y == 1)
{
//Do stuff to fall
}
Your answer
Follow this Question
Related Questions
character gravity problem 1 Answer
Help! Problems with Character.isGrounded in Unity 4.0.1? 3 Answers
Moving Platform and CharacterController.Move not working. The player falls 3 Answers
Character collider / physics bug in Unity 3.3 - falling through terrain / objects 5 Answers
strange character controller falling 1 Answer