Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JayFitz91 · Aug 17, 2015 at 09:34 AM · collisionraycastjump

Jumping with raycasts causing issues?

I have the following code which handles jumps and double jumps:

 void JumpingControls()
 {
     //If space is pressed and isGrounded
     if (Input.GetButtonDown("Jump"))
     {
         if (isGrounded && !attacking)
         {
             animate.SetBool("Jump", true);
 
             //set grounded to false
             isGrounded = false;
 
             //Apply jumpForce upward
             GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
 
             //allowed to double jump
             doubleJump = true;
 
         }
         else
         {
             if (doubleJump)
             {
                 animate.SetTrigger("Double");
                 //not allowed to double jump
                 doubleJump = false;
 
                 //Apply jumpForce upward
                 GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
             }
         }
     }
 }
 
 void CheckIfGrounded()
 {
     if (isGrounded)
     {
         animate.SetBool("Jump", false);
         doubleJump = false;
     }
 }

This code works great when I am detecting if I am grounded or not, but only with this collision code:

 void OnCollisionEnter(Collision col)
 {
     if (col.contacts.Length > 0)
     {
         ContactPoint contact = col.contacts[0];
         if (Vector3.Dot(contact.normal, Vector3.up) > 0.5f)
         {
             isGrounded = true;
         }
         else
             isGrounded = false;
     }
 }

But there is issues with this code, if i collide with most things, it sets my isGrounded script to false, which doesnt allow me to jump, so I want to handle jump using a raycast pointing down from my feet but the issue is when I jump with the raycast code, it exits the Jump state immediately and doesn't allow me to double jump even though it detects that I am not grounded correctly.

I need a second pair of eyes on this because I am stumped, any help appreciated, here is the raycast code:

 void DetectCollision()
 {
     RaycastHit hit;
     float dist;
     Vector3 dir;
 
     dist = 1.5f;
     dir = new Vector3(0, -1, 0);
 
     //edit: to draw ray also//
     Debug.DrawRay(transform.position + new Vector3(0, 1.0f, 0), dir * dist, Color.green);
     //end edit//
 
     if (Physics.Raycast(transform.position + new Vector3(0, 1.0f, 0), dir * dist, out hit, dist))
     {
         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
Best Answer

Answer by JayFitz91 · Aug 17, 2015 at 05:31 PM

I manage to fix it, i must have had conflicting lines of code somewhere, but I converted everything into one function which checks if I am colliding and it works now.

Here is the code I am using:

 void JumpingControls()
 {
     RaycastHit hit;
     float dist;
     Vector3 dir;
 
     dist = 1.5f;
     dir = new Vector3(0, -1, 0);
 
     //If space is pressed and isGrounded
     if (Input.GetButtonDown("Jump"))
     {
         if (isGrounded && !attacking)
         {
 
             //set grounded to false
             isGrounded = false;
 
             //Apply jumpForce upward
             GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
 
             //allowed to double jump
             doubleJump = true;
 
         }
     }
 
     if (Physics.Raycast(transform.position + new Vector3(0, 1.0f, 0), dir * dist, out hit, dist))
     {
         isGrounded = true;
         animate.SetBool("Jump", false);
     }
     else
     {
         isGrounded = false;
         animate.SetBool("Jump", true);
 
         if (Input.GetButtonDown("Jump") && doubleJump)
         {
             animate.SetTrigger("Double");
             //not allowed to double jump
             doubleJump = false;
 
             //Apply jumpForce upward
             GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
         }
     }   
 }

Comment
Add comment · Show 1 · 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 Lemonade · Aug 17, 2015 at 08:37 PM 0
Share

Great ! Glad to hear that :)

avatar image
0

Answer by Lemonade · Aug 17, 2015 at 10:42 AM

I think the problem is you are offsetting the origin of the Raycast.

 transform.position + new Vector3(0, 1.0f, 0)

If you move it out of the object it could hit its own collider. However if you are sure that the origin did not move out of the collider I would try to turn off all colliders around except ground collider and debug what your method returns.

Comment
Add comment · Show 1 · 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 JayFitz91 · Aug 17, 2015 at 11:00 AM 0
Share

I have his collider set to ignore raycasts so i don't think that's the issue, but I tried it just to be sure, and still the same issue, I moved the collider up as a check like so but still the issue persists unfortunately

alt text

capture.png (160.7 kB)

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

26 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

Related Questions

Physics2D Raycast stops detecting collision 0 Answers

edit a variable as a result of a collision 0 Answers

character jump on collision with a box 0 Answers

Custom Collision Detection 4 Answers

Collision Detection If Raycast Source Is inside A Collider? 4 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