Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kfchristian15 · Feb 06, 2015 at 09:55 PM · c#raycastjumpscriptingbasicsground

RayCast to test if player is grounded

So I've been using an isGrounded variable to limit jumping to only when on the ground, but if the player walks off of a platform, then they can jump in the air. I did some research and it seemed like people used a raycast to check whether or not the player was grounded. I've never used a raycast before so this is my first attempt and unity shows nothing as wrong in the script, but I can't move when I play the scene. I assume that's because my if statement is never true, but I don't know how to fix this as I used the unity scripting API as my example. I want it so the raycast is constantly happening and isGrounded is set to true only when the raycast hits the floor below the player. Please let me know what you think I should change to make this work, any input is welcome. Thank you!

 void Start () {
     distToGround = collider.bounds.extents.y;
 }
 bool IsGrounded()
 {
     return Physics.Raycast(transform.position, - Vector3.up, distToGround + 0.1f);
 }
 public void NoMove()
 {
     movement=false;
 }
 void FixedUpdate () 
 {
     if (IsGrounded())
     {
         speedH=30;
     }
     if (!IsGrounded())
     {
         speedH=3;
     }
     }
 void Update ()
 {
     if (movement)
     {
         if (Input.GetKey (KeyCode.RightArrow))
         {
             animation.Play ("Allosaurus_Walk");
             rigidbody.AddForce(Vector3.right *speedH);
         }
         if (Input.GetKeyUp (KeyCode.RightArrow))
         {
             animation.Stop ("Allosaurus_Walk");
             animation.Play ("Allosaurus_Idle");
         }
         if (Input.GetKeyUp (KeyCode.LeftArrow))
         {
             animation.Stop ("Allosaurus_Walk");
             animation.Play ("Allosaurus_Idle");
             if (Input.GetKeyUp (KeyCode.LeftArrow))
             {
                 transform.Rotate (0,180,0);
             }
         }
         if (Input.GetKey (KeyCode.LeftArrow))
         {
             animation.Play ("Allosaurus_Walk");
             rigidbody.AddForce(Vector3.left *speedH);
             if (Input.GetKeyDown (KeyCode.LeftArrow))
             {
                 transform.Rotate (0,180,0);
             }
         }
         if (moveUp && IsGrounded())
         {
             animation.Play ("Allosaurus_JumpRight");
             rigidbody.AddForce(Vector3.up * speedZ);
         }
         if (Input.GetKey ("d"))
         {
             animation.Play ("Allosaurus_Run");
             rigidbody.AddForce(Vector3.right *speedH);
         }
         if (Input.GetKeyUp ("d"))
         {
             animation.Stop ("Allosaurus_Run");
             animation.Play ("Allosaurus_Idle");
         }
         if (Input.GetKeyUp ("a"))
         {
             animation.Stop ("Allosaurus_Run");
             animation.Play ("Allosaurus_Idle");
             if (Input.GetKeyUp ("a"))
             {
                 transform.Rotate (0,180,0);
             }
         }
         if (Input.GetKey ("a"))
         {
             animation.Play ("Allosaurus_Run");
             rigidbody.AddForce(Vector3.left *speedH);
             if (Input.GetKeyDown ("a"))
             {
                 transform.Rotate (0,180,0);
             }
         }
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
Best Answer

Answer by mattyman174 · Feb 06, 2015 at 10:02 PM

You should only be casting a Ray when the Player wants to jump, not at a fixed interval or worse yet, on every frame in Update.

Ray-casts are expensive for the most part and should be used sparingly and with good reason.

When the Player hits the jump key, do a ray-cast to check if they are grounded, if they are not then do nothing, else allow them to jump.

This avoids constantly casting rays to check if the player is on the ground every frame.

Comment
Add comment · Show 9 · 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 kfchristian15 · Feb 06, 2015 at 10:47 PM 0
Share

Thanks for the advice, I"ll consider doing that. The raycast code itself still isn't functioning though, any ideas on why the code isn't working? Thanks for your input, I appreciate it.

avatar image mattyman174 · Feb 07, 2015 at 12:42 AM 0
Share

You havent accounted for the Players height when casting the ray i would imagine, the transform position is usually at the center of an Object, not its base.

avatar image kfchristian15 · Feb 07, 2015 at 01:29 AM 0
Share

Good thought, I just changed the number, but the character still can't move. $$anonymous$$aybe there's multiple reasons?

avatar image mattyman174 · Feb 07, 2015 at 06:01 AM 0
Share

If your not able to move then the issue must be in another area of code as there is nothing in what you have given that would cause you to lose mobility.

avatar image kfchristian15 · Feb 07, 2015 at 11:49 PM 0
Share

Here's the full code. I didn't include it initially because I thought that the raycast was the problem, but maybe it isn't. Please let me know if you see anything and thanks for your suggestions this far.

Show more comments

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump/grounding bugs, advice appreciated 1 Answer

Raycasting not turning off jump. Would really love some help on my code. 2 Answers

2D C# Jump Script Addforce 2 Answers

Spawn Ground right next to current ground 1 Answer

Still constant jumping even with raycasting. 0 Answers


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