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 TelionFondrad · Jan 12, 2021 at 10:01 PM · 3dcharactercontrollerjumpjumpingcharacter controller

Jumping not always work

Got the following script off the Character Controller doc page and it's working only sometimes. I disabled rigid body because it was messing with the rotation of the object, making it rotating like wild. Below is the code:

 public class PlayerController : MonoBehaviour
 {
   private CharacterController controller;
   private Vector3 playerVelocity;
   private bool groundedPlayer;
   private float playerSpeed = 2.0f;
   private float jumpHeight = 1.0f;
   private float gravityValue = -9.81f;
 
   private void Start()
   {
     controller = gameObject.AddComponent<CharacterController>();
   }
 
   void Update()
   {
     groundedPlayer = controller.isGrounded;
     if (groundedPlayer && playerVelocity.y < 0)
     {
       playerVelocity.y = 0f;
     }
 
     Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     controller.Move(move * Time.deltaTime * playerSpeed);
 
     if (move != Vector3.zero)
     {
       gameObject.transform.forward = move;
     }
 
     // Changes the height position of the player.. 
     if (Input.GetButtonDown("Jump") && groundedPlayer)
     {
       print("Jump button pressed");
       playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
     }
 
     playerVelocity.y += gravityValue * Time.deltaTime;
     controller.Move(playerVelocity * Time.deltaTime);
   }
 }

The jumping here only works sometimes. The object is not always considered grounded so when the jump button pressed it's usually ignored. Though the object is constantly changing state and sometimes actually gets grounded so that jump triggers. Also, all objects are simply set in the scene in 10 by 10 area, with the player object being in 0 0 0 coordinates, yet it moves a bit when I run the game, I feel like that could cause the issue here.

Any suggestions?

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
0

Answer by dtbrown0801 · Jan 13, 2021 at 12:26 AM

Hi. Did it work with the rigid body enabled? Because if it did in the constraints you can freeze the rotation of all axis and the position of every axis except the y (unless you want it to move around strangely).

Comment
Add comment · 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
0

Answer by TelionFondrad · Jan 13, 2021 at 04:29 PM

Hi, it's still jumping only sometimes, while spinning wild. I returned back the rigidbody component as the dtbrown0801 recommended. Check out the console. It logs when Jump button is pressed and when jump is actually initiated. I also checked the .isGrounded value on every frame. Apparently it looks like true false false false... true... etc. Basically, it's changing its state for some reason all the time. Why would that happen? alt text

Vid: https://i.gyazo.com/7596d77cb54349ca95f57f44e72a7aef.mp4

Comment
Add comment · 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

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

161 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Player Inconsistent Jumps 0 Answers

How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers

change jumping in the character controller 0 Answers

How to limit Jump Air Control (air surfing) 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