Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 alexkarak · May 23, 2019 at 10:43 AM · raycastjumpisgrounded

ground check isnt working...HELP!

hey guys ...i have this script attached to a ragdoll character but when i jump he moves up without changing the is grounded bool to false so he keeps floatin upwards .....how to fix this?

 using UnityEngine;
 public class Jump : MonoBehaviour
 {

 bool isgrounded = true;
 // Start is called before the first frame update
 void Start()

 {

 }

 // Update is called once per frame
 
 void Update()
 {
     GroundCheck();
     if (Input.GetButtonDown("Jump") & (isgrounded == true))

         GetComponent<ConstantForce>().force = new Vector3(0, 150, 0);
     
     if(isgrounded == false)

         GetComponent<ConstantForce>().force = new Vector3(0, 80, 0);

 }
 void GroundCheck()
 {
     RaycastHit hit;
     float distance = 3f;
     Vector3 dir = new Vector3(0, -2);

     if (Physics.Raycast(transform.position, dir, out hit, distance))
     {
         isgrounded = true;
     }
     else
     {
         isgrounded = false;
     }
 }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MadDevil · May 23, 2019 at 11:00 AM

     bool isgrounded = true;
     // Start is called before the first frame update
     void Start()
     {
     }
     // Update is called once per frame
 
     void Update()
     {
         GroundCheck();
         if (Input.GetButtonDown("Jump") & (isgrounded == true))
             GetComponent<ConstantForce>().force = new Vector3(0, 150, 0);
 
         if (isgrounded == false)
             GetComponent<ConstantForce>().force = new Vector3(0, -80, 0);
     }
     void GroundCheck()
     {
         RaycastHit hit;
         float distance = 3f;
         Vector3 dir = new Vector3(0, -2);
         if (Physics.Raycast(transform.position, dir, out hit, distance))
         {
             isgrounded = true;
         }
         else
         {
             isgrounded = false;
         }
     }

You just have to give negative value to your downforce in update when isgrounded is false

Comment
Add comment · Show 2 · 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 alexkarak · May 23, 2019 at 01:04 PM 0
Share

yeah but the "is grounded" bool isnt turning false even though my character is in the air

avatar image MadDevil alexkarak · May 24, 2019 at 03:09 AM 0
Share

The code that i have posted does make the bool false. $$anonymous$$ake the bool public and watch in the inspector. Whenever you will press the space bar your character will jump and the boolean is false while its in air, For Testing purpose I have taken a cube as character and a plane below it for ground. The script is attached to the cube along with constant force component.

avatar image
1

Answer by Captain_Pineapple · May 23, 2019 at 11:02 AM

Hey there,

i guess that if you add a Debug.Log to output what object you actually hit you will see that it's the player object itself.

Add a Layermask to disable your raycast from hitting anything else then the environment.


Is there a specific reason why you do not want to use the builtin gravity? As @MadDevil pointed out your force vector has the wrong direction. You can avoide these things by keeping it simple and sticking to the standart gravity.

Comment
Add comment · Show 2 · 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 alexkarak · May 23, 2019 at 01:02 PM 0
Share

@Captain_Pineapple ....i wanna make a physics based controller with an active ragdoll and this force makes the player stand on his feet thats why is positive ...how can i make the raycast hit only the floor or anything with a specific tag and not every collider?

avatar image Captain_Pineapple alexkarak · May 24, 2019 at 08:46 AM 0
Share

Assign Layers your player objects to a "Player-Layer". (Somewhere in the top right corner there is a layer button. - there is also a ton of tutorials on this)

Then you can create a public Layermaskon your player script where you can select certain layers. This mask can be given to a raycast as parameter to exclude given layers from the raycast. Scripting reference can by found here. This way you can make sure the raycast ignores everything on the playerlayer.


Carefull though: If you add multiple Players they will not be able to stand on each other.

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

156 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 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 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 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 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 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 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 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

Raycast2D Only returns true 1 Answer

UI jump button and isgrounded using Rigidbody 1 Answer

Raycast always colliding (SOLVED) 1 Answer

Cant jump while running left or right c#? 3 Answers

isGrounded not working 2 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