- Home /
Making a rolling ball jump, but only when it is touching the ground
I know that a lot of these questions have already been asked, but I have looked through many, and none of them seem to work for me. I have a ball that is rolling, so it's y axis is not always straight up. I need it to increase it's y axis (global). I have gotten this to work, but as soon as I add in the character controller, it stops jumping. Here is my code, any help is appreciated.
This section is under Update, and the groundContact variable is set.
Code is in C#
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
groundContact = true;
}
if (Input.GetKeyDown(KeyCode.Space) && groundContact)
rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
Just making sure, so what you are saying is that I will have to use another way to set groundContact to true other than CharacterController to get this to work correctly? If so, is a rigid body the best way to do this?
Answer by Kadeo · Jun 08, 2016 at 10:58 AM
Thank you everyone for helping me, I have figured it all out, it works great now. I have ended up using a rigidbody collider and trigger to get it to jump only when touching the ground. All of the replies so far has given me a little advice that has helped me figure it out. I will post my code below just in case someone else needs it.
Code is in C#
void Update()
{
if (Input.GetKey (KeyCode.Space) && groundContact)
{
rb.AddForce (new Vector3 (0, 10, 0), ForceMode.Impulse);
groundContact = false;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Ground"))
{
groundContact = true;
}
}
Now to get to the finer details of the jump.
Answer by nelson218 · Jun 08, 2016 at 03:51 PM
Simply use these
groundContact = controller.isGrounded;
instead of these
if (controller.isGrounded) groundContact = true;
Answer by melwei · Jun 07, 2016 at 04:33 PM
I think you should set groundContact to false at the end of your Update, or it will continouosly be true after jumping once. If controller.isGrounded doesn't work (Test with Debug?), try to shoot a raycast down.
Answer by IAcontrol · Jun 07, 2016 at 07:05 PM
public Transform GroundCheck;
public LayerMask WhatIsGround;
float GroundRadius = 0.2f;
bool Grounded = false;
void FixedUpdate ()
{
Grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, WhatIsGround);
}
I used this code for make my 2D game, when the sprite touch the floor set the bool true. You have to create an empty child game object.
Answer by solo365 · Jan 07, 2018 at 05:54 PM
I am quite new to this field.Can you please tell me how can I do the same thing with my code. I also want my sphere to jump again after it touches the ground. Here's my code.
void Update()
{
if (
!thrown && (
(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ) ||
Input.GetMouseButtonDown(0) )
)
{
rb.isKinematic = false;
rb.AddForce (new Vector3(0.0f, 0.5f, 0.2f));
thrown = true ;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("player"))
{
thrown = true;
}
}
Your answer
Follow this Question
Related Questions
Using isGrounded with a RigidBody 2 Answers
How to make character controller forward jump a certain distance? 1 Answer
Why does my characterController code not apply gravity correctly? 0 Answers
How can I edit this script, that it will check if the character is standing on a specific layer? 0 Answers