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 stefo66 · May 02, 2014 at 11:03 PM · collisionraycastjumping

Raycast vs OnCollisionStay in order to test for the ground drastically changes my jumping behavior

So I've been coding a character for a day or two now as one of my first proper unity ventures, and I'm having issues with the jumping. I opted to do it such a way that the player can only jump when he is not already jumping, and this is done by setting a jump variable to true when he jumps, and only setting it to false when he touches the floor again. While this is true, he cannot jump. EZMode.

The main key to the issue for me is getting the collision down in order to set the "jumping" variable to true. Initially I did this via an OnCollisionStay code, which can be seen in the code below. This worked pretty fantastic, only it meant that anything I wanted to be able to stand on and then jump off again (Crates for example) had to be marked as ground, and then I got funny behavior when I was standing against (and therefore colliding with) objects and holding the jump button.

I then moved onto raycast, and this also worked for the sake of changing the variable, however suddenly the jumping is drastically altered. Now I jump approx 10-20x higher, and I jump in such a way that just tapping the space bar is a tiny jump and holding it is a massive one. And also changing the jumps strength doesnt seem to change things and much as it did before.

My question really is twofold. Number 1, why on earth does something as simple as changing the way I'm detecting a collision make such a huge difference on the actual jumping? I'm having the player jump by increasing his velocity on the y axis, and to be perfectly honest I cannot see how a raycast would affect velocity. I've done a debug, and the "jumping" variable changes to true literally the instance I press the jump button (I thought perhaps the jump variable was staying false too long as a result of the raycast or something). Things I have tried: Placing the raycast in Update rather than FixedUpdate, placing it above and below the jumpcode, increasing and decreasing the distance of the raycast.

Number 2, is there a simpler way to do this so that I can stand and then jump off obstacles but not fly up them? Am I going about this all wrong?

Code below. The commented code works, but I edited it out for the raycast. If there are unusual formats/lines, its because I've been shifting it around so much.

 using UnityEngine;
 using System.Collections;
 
 public class CharJumping : MonoBehaviour {
 
 
 
     public float jumpStr;
     private bool jumping;
     private Vector3 jumpVel;
 
     void FixedUpdate () 
     {
 
         Debug.Log (jumping);
 
         Vector3 jumpVel = new Vector3 (0, jumpStr, 0);
 
         if (Physics.Raycast (transform.position, Vector3.down, 1.0f)) 
         {
             jumping = false;
         }
 
         if (Input.GetAxis("Jump") !=0 && jumping == false) 
         {
             jumping = true;
             rigidbody.velocity = rigidbody.velocity + jumpVel;
         }
     }
 
     //void OnCollisionStay(Collision other) {
     //    if (other.gameObject.tag == "Ground") {
     //        jumping = 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jeff-Kesselman · May 02, 2014 at 11:47 PM

Its because your ray is still hitting the ground after you leave it. So, you are getting another "jump" when in mid air.

Your "jumping' variable isn't helping you because yo hare setting it false whenever the ray hits ground, which means whenever you are over the ground.

Rather then using a fixed 1.0f distance for your ray, you need to limit the ray to the height of your object, which would be the distance between your ray origin in world space and its render.bounds.min.y, assuming Y is up.

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 stefo66 · May 03, 2014 at 09:36 AM 0
Share

Okay so here's what I did,

I created a new float variable (raycastLength) and placed this within the fixed update

 raycastLength = transform.position.y - collider.bounds.$$anonymous$$.y;

I had to use collider because my character has no physical "being", its just a collider a rigidbody and a camera.

I then changed the 1.0f to rayCastLength.

This may have worked, I'm going to playtest it some more and see if I can get the behaviour I want from it with appropriate jump speeds.

EDIT: So my jumpStr variable now has to be 5 (ins$$anonymous$$d of the 2.5 it was with the OnCollisionStay) but other than that, the jump behaviour is pretty ordinary. It seems that I could also have used a print or a debug.log to physically work out the transform.position.y and bounds.$$anonymous$$.y and worked it out myself. For example, I now know its 0.16 and I can't really see a particular reason that I cannot change substitute "raycastLength" with 0.16f, other than the fact that the player object can now be resized without worry.

avatar image Jeff-Kesselman · May 03, 2014 at 07:52 PM 0
Share

Glad it helped!

I had a similar bug once in a 2D game so I recognized the symptoms right away :)

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

21 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

Related Questions

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

How to detect which side is the raycast hitting? 2 Answers

My Raycast acts like it's hitting something when it isn't 0 Answers

Creating custom character controller collision checking 0 Answers

Jumping on top of an enemy problem 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