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 /
This question was closed Jan 16, 2018 at 04:02 AM by technano for the following reason:

I guess the reason this code doesn't work is simply because there is no 2D raycast that returns a bool. So it would have to be re structured completely

avatar image
0
Question by technano · Jan 11, 2018 at 02:37 AM · jumpjumpingcharacter controller

Why won't this jump code work?

I've been following this tutorial on making a character controller and I'm trying to get the player to jump but only when it's grounded. Here is the code.

 public class WalkingController : Controller
 {
     //Movement information
     Vector3 walkVelocity;
     Vector3 previousWalkVeloctiy;
     float adjVertVelocity;
     float jumpPressTime;
 
 
     //Setting
     public float walkSpeed;
     public float jumpSpeed;
 
 
     public override void ReadInput(InputData data)
     {
         previousWalkVeloctiy = walkVelocity;
         ResetValues();
 
         //set horizontal movement.
         if(data.axes[0] != 0f)
         {
             walkVelocity += Vector3.right * data.axes[0] * walkSpeed;
         }
 
         //set jump movement.
         if (data.buttons[0] == true)
         {
             if (jumpPressTime == 0f)
             {
                 if (Grounded())
                 {
                     adjVertVelocity = jumpSpeed;
                 }
             }
             jumpPressTime += Time.deltaTime;
         }
         else
         {
             jumpPressTime = 0f;
         }
 
 
         newInput = true;
     }
 
     //method that looks below the player to see if something is there.
     bool Grounded()
     {
         return Physics.Raycast(transform.position, Vector3.down, coll.bounds.extents.y + 0.1f);
     }
 
     void ResetValues()
     {
         walkVelocity = Vector3.zero;
         adjVertVelocity = 0f;
     }
 
     void LateUpdate()
     {
         if (!newInput)
         {
             previousWalkVeloctiy = walkVelocity;
             ResetValues();
             jumpPressTime = 0f;
         }
 
         /*
          * FYI:
          * Here we set the Vectors to the velocity of the rigidbody. Setting the Y value of
          * the new Vector2 Means that the player will fall down normally. If set to 0 it would
          * either keep the player from falling or make them fall very slowly.
          */
         rb.velocity = new Vector3(walkVelocity.x, rb.velocity.y + adjVertVelocity);
         newInput = false;
     }
 
 }

The tutorial is more designed for a 3d game, and I put this character controller on a cube just to see what would happen and it worked fine. I'm not sure why it wouldn't work on a 2D sprite as well. I even tried changing the raycast to be a 2D raycast, but that didn't work to well because Raycast2D isn't a bool like Physics.Raycast is. Any explaination would be greatly appreciated! :D

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

  • Sort: 
avatar image
0

Answer by Rickasheye · Jan 11, 2018 at 02:40 AM

You put the jumpPressTime += Time.deltaTime;

if(Grounded()){ jumpPressTime += Time.deltaTime; adjVertVelocity = jumpSpeed; }

i think

Comment
Add comment · Show 4 · 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 Rickasheye · Jan 11, 2018 at 02:41 AM 0
Share

or the adjVertVelocity = jumpSpeed; the jump $$anonymous$$ethod idk if so take the = from it and make it += idk

avatar image technano · Jan 12, 2018 at 12:27 AM 0
Share

So I tried this out and the Grounded method isn't even being reached. I have no Idea why it wouldn't be.

avatar image Rickasheye technano · Jan 12, 2018 at 07:41 AM 0
Share

you have the jump button then the grounded method inside maybe if you put that outside and put the jump button on the inside you might be alright e.g.

 if(Grounded == true){
 if(input.getkey(keycode.e){
 //jump thing
 }
 }

Why do you have Grounded as true why not .isGrounded?

avatar image technano Rickasheye · Jan 12, 2018 at 03:51 PM 0
Share

I just tried switching them around and that didn't work. I'm pretty sure the issue is with the Physics.raycast in the grounded method. It should be returning true so that the if statement works.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

Jumping not always work 2 Answers

,How to disable autojumping when holding spacebar? 4 Answers

How to limit Jump Air Control (air surfing) 2 Answers

I can't do that my character jumps while running 0 Answers

Best way to implement jump method for character controller 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