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 mattking99 · Feb 01, 2020 at 11:49 PM · jumpjumpingglitch

Auto Jump on 3d platform glitch

Hi, I'm a beginner and I'm trying to do a simple autojump, but I got some glitches. Here is the glitch: it jumps some times lot more than it should, sometimes lot less, and always at different altitude. Here is my script: (ofc I have all "using...")

 public class PlayerController : MonoBehaviour
 {
   
     public float moveSpeed;
     public float JumpForce;
     private Rigidbody rig;
     
     
     void Awake()
     {
         // get the components
         rig = GetComponent<Rigidbody>();
 
     }
 
     void Update()
     {
 
         move();
         if (Input.GetKey("space"))
         {
             tryJump();
         }
         //else if (Input.GetButtonDown("Jump"))
           //  tryJump();
         
 
 
     }
     void tryJump()
     {
         
         Ray ray1 = new Ray(transform.position + new Vector3 (0.50f,0,0.5f), Vector3.down);
         Ray ray2 = new Ray(transform.position + new Vector3(0.5f, 0, -0.5f), Vector3.down);
         Ray ray3 = new Ray(transform.position + new Vector3(-0.5f, 0, 0.5f), Vector3.down);
         Ray ray4 = new Ray(transform.position + new Vector3(-0.5f, 0, -0.5f), Vector3.down);
 
         bool cast1 = Physics.Raycast(ray1, 0.50f);
         bool cast2 = Physics.Raycast(ray2, 0.50f);
         bool cast3 = Physics.Raycast(ray3, 0.50f);
         bool cast4 = Physics.Raycast(ray4, 0.50f);
 
         if (cast1 || cast2 || cast3 || cast4)
             rig.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
 
     }
 
    void move ()
     {
         
         float xIN = Input.GetAxis("Horizontal");
         float zIN = Input.GetAxis("Vertical");
 
         Vector3 dir = new Vector3(xIN, 0, zIN) * moveSpeed;
         dir.y = rig.velocity.y;
         rig.velocity = dir;
         
     }


I don't really know if this is the best way for jump, but it should still work I guess. So, why if I use Input.GetButtonDown("Jump") it's ok? What do I have to add in the code? Thank you!

Comment
Add comment · Show 6
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 Magso · Feb 02, 2020 at 01:00 AM 0
Share

You do have if(Input.Get$$anonymous$$ey("space")) calling tryJump() every frame space is pressed, maybe that's the issue. Use Input.Gey$$anonymous$$eyDown ins$$anonymous$$d.

avatar image mattking99 Magso · Feb 02, 2020 at 10:28 AM 0
Share

Hi, Thanks for the answer, but nope, it doesn't work, because Get$$anonymous$$eyDown get only first frame, so if I keep space pressed, my character doesn't autojump, he jump just the first time when I press it (the description of Get$$anonymous$$eyDown is "returns true during the frame the user starts pressing down the key", while the description of Get$$anonymous$$ey is "returns true while the user holds down the key"). How should I fix this? Could it be a problem with Ray or with physics in Unity or other things maybe? Thanks again!

avatar image Magso mattking99 · Feb 02, 2020 at 10:42 AM 0
Share

Ah, I misunderstood. It would be better to use isGrounded to check if the player has landed before jumping.

 if(Input.Get$$anonymous$$ey("space") && GetComponent<CharacterController>().isGrounded)
     {
     tryJump();
     }


Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LeFlop2001 · Feb 02, 2020 at 04:30 PM

Im pretty sure you’re having this issue because you’re using getKey and not getKeyDown. So instead of adding the force on the frame you press the button, it continues adding force till the raycasts no longer hit. This is different based on the situation.

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 mattking99 · Feb 02, 2020 at 05:41 PM 0
Share

Hi, maybe I said that badly, I wanna make an autojump system: when the player hold down "space", it jump again, again and again, until he stops. The problem with get$$anonymous$$eyDown is that the first frame when you start holding space is the input. In that case I call the function only one time (the first frame) and that's ok but for a normal jump system, not for an autojump. (Btw I tried, not telling you something I didn't try)

$$anonymous$$aybe you can still do it with get$$anonymous$$eyDown but you have to do lot more useless work (as you have only 1 frame as input you have to create bool isJumping, float maxAltitude, bool $$anonymous$$ax_Height_Reached and you want to move the transform.position with a float gravity etc.)

With get$$anonymous$$ey you have as input all frames where you hold "space", so I have only to check if the character is on the ground or not.

To be clearer I will put a gif with the glitch:

alt text

As you can see, the character does autojump (I was holding "space") but at different altitude. Is it caused by the update function? because it depends on pc's performance and not on real ti$$anonymous$$g and maybe sometimes the input get 2-3 times in 1 frame? I really don't know what is the problem of that simple code. In my code the raycast hit only when the character hits the ground, so the hitbox is correct. The force is added only when ray (boolean) hits the ground, so midair is not a problem (in theory) Hope that explanation was better. Thanks anyway!

ezgifcom-optimize.gif (501.6 kB)
avatar image LeFlop2001 mattking99 · Feb 02, 2020 at 08:48 PM 1
Share

What I meant regarding the raycasts is that because the raycasts have a length the jump gets inconsistent. After jumping you add the force shooting it upwards but based on what velocity or momentum the body already had it could happen that during the next frame the body is still close enough to the ground for the raycasts to hit. Thus it adds the force again. I would suggest using the oncollision function and a simple bool to handle the autojump

 bool grounded = true;
 
 void Update()
 {
      If(grounded&&Input.get$$anonymous$$ey(keyCode.space))
      {
          rig.AddForce(Vector3.up)
          grounded =false;
      }
 }
 void OnCollision(Collider collision)
 {
       If(collision.transform.gameObject.tag = „floor“)
       {
            grounded = true;
       }
 }


avatar image mattking99 LeFlop2001 · Feb 03, 2020 at 08:44 AM 0
Share

Truee I will try today. Just to know, is there a way to make it not based on frame but on time? Like Time.deltaTime or something like that ? If so where should I add it? Thanks so much, trying today

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

123 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

Related Questions

Adding a jump feature help? 2 Answers

Jumping causes strange bugs and messages 3 Answers

Others can't jump in my game 0 Answers

Anti-Gravity isn't working 1 Answer

Hey Guys I have a problem with a single jump script. 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