Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by _aex · Jul 05, 2015 at 10:32 AM · collisionjumping

SweepTest to check if grounded

     void FixedUpdate () {
 
         if (Input.GetKeyDown (KeyCode.Space) && IsGrounded ())
             Jump ();
 
         rigidbody.MovePosition (transform.position + heading * Time.deltaTime);
     }
 
     void Jump () {
 
         rigidbody.AddForce (Vector3.up * jumpForce, ForceMode.Impulse);
     }
 
     bool IsGrounded () {
         RaycastHit hit = new RaycastHit ();
         return rigidbody.SweepTest (Vector3.down, out hit, 0.1F);
     }

Hi! Really quite new to Unity here and I'm experimenting a little with different things.

I would like to make my character able to jump but only when actually on the ground.

Previously I created a "Feet" Quad GameObject and childed it to the player, and used SendMessage to make the player GameObject set a 'grounded' variable to true when OnTriggerEnter was called. However, the solution felt a bit 'clunky' and often the controls felt a bit 'sticky'.

I figured this was because of some sort of desync between the Feet Quad and the Player GameObject. After a bit of Googling I came across many, many examples calling for a RayCast downwards. However I'm not satisfied by that since it leave 'holes' in the area I want to check. So I found out about SweepTest and decided to give it a try.

However, my IsGrounded () function never returns true, thus sticking my player to the ground :(

Does anyone have any idea why this may be?

Notes: My player's Rigidbody has constrained rotation (though I tried turning that off so this isn't the problem, probably) and a BoxCollider attached to it. The ground is a primitive Plane. Thinking that might have been the problem I dropped my player onto a primitive Cube, but it refused to jump even then.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryBurritoCoder · Jul 05, 2015 at 12:01 PM

Im not sure what you mean by "Holes" when using raycast.

You should ( or can ) use a normal raycast downwards like this

 RaycastHit hit;
 
 bool isGrounded = false;
 
 // ( ATTENTION: if your player transform is in the middle of the character, then add the half height of your player to your maxRayDistance)
 float maxRayDistance = 0.2f; //You can change how much down checks
 
 public LayerMask layerToCheck; // In inspector you can tick which layer should be checked by the raycast, you can change objects layer in inspector and add new layers 
 
 void Update()
 {
    CheckIfGrounded(); // Constantly check if grounded
 }
 
 void CheckIfGrounded()
 {
     if(Physics.Raycast(transform.position, Vector3.down,out hit, maxRayDistance,layerToCheck))
     {
         //If an object with this layer is being hit then make isGrounded true
         isGrounded = true;
     }
     else // Else if not hitting anything, set isGrounded false
     {
         isGrounded = false;
     }
 }


And then you can do if input and if isGrounded, you can jump

hope it helps

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _aex · Jul 05, 2015 at 12:40 PM 0
Share

What I meant by "holes" is that doing a single raycast like this can result in situations like this:

http://puu.sh/iNS7h/365c461823.png

which is undesirable.

Even if I were to create a RayCast in all four corners there would still be moments, for example if the player finds themselves on a very thin tightrope, during which the player could not jump despite being grounded:

http://puu.sh/iNSdE/3f6b656892.png

This is why I would like to do what's basically a "square cast" or "cube cast" downwards.

avatar image AngryBurritoCoder · Jul 05, 2015 at 01:05 PM 1
Share

i see what you mean, well you also have the option of using the collider on your character as the thing that checks for ground. Basically you check if your character collider is colling with anything from bottom to something like 0.2f of height up the collider and if it is ground or anything you can jump off then you make isGrounded = true.. Or you can use character controller xd, i was always against rigidbody controllers unless it uses physics for most of the game.

avatar image _aex · Jul 05, 2015 at 03:13 PM 0
Share

I'll definitely try this, thanks :)

Also I wanted to use Rigidbody because a) I'd like some level of ragdoll physics later during explosions for example and b) I wanted some kind of an arc to the jump.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Delay Jumping 3 Answers

Box collider didn't jump with player 1 Answer

Object jumps through roof when holding "Up" button? 1 Answer

My jump code seems to be broken, the character's flags for jumping appear to be working strangely. 1 Answer

Detecting Collision for Jumping 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges