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 Smasham · Aug 12, 2019 at 07:19 PM · rigidbodyvelocitydrag

Random velocity changes

I have a character that moves with rigidbody. When i jump, i looks find, but sometimes, my character boost up while in the air and get out of the map very fast. I checked, and when it happens, it says the velocity is at about 210 or around it. i just happen when i walk and i jump. It happen like 5% of the time. why is it happening and how can i fix it ?`using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class playerManager : MonoBehaviour
 {
 float xMovement;
 float zMovement;
 float xPosition;
 float yPosition;
 float zPosition;
 public float runningSpeed;
 public Animator animator;
 public Camera camera;
 public float cameraHeight;
 public float cameraAngle;
 public bool isOnGround;
 public float jumpForce;
 Rigidbody playerRigidbody;
 public float gravityMultiplier;
 Vector3 vel;
 public float airSpeed;
 public float groundSpeed;
 public bool canJump = true;
 
 void Start()
 {
     playerRigidbody = GetComponent<Rigidbody>();
     vel = playerRigidbody.velocity;
 }
 
 void Update()
 {
     
     xPosition = transform.position.x;
     yPosition = transform.position.y;
     zPosition = transform.position.z;
     PlayerMovements();        
     CameraManager();
 }
 
 
 public void CameraManager()
 {
     camera.transform.position = new Vector3(xPosition+cameraAngle,cameraHeight,zPosition+cameraAngle);
 }
 
 
 public void PlayerMovements()
 {
     //Jump
     if(Input.GetKeyDown(KeyCode.Space) & isOnGround == true & canJump == true)
     {
         runningSpeed = airSpeed;
         canJump = false;
         playerRigidbody.velocity = new Vector3(xMovement,0,zMovement);
         playerRigidbody.angularVelocity = Vector3.zero;
         playerRigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
         playerRigidbody.AddForce(transform.up * jumpForce);
         playerRigidbody.drag = 0;

     }
     
     
     //Move the Character
     if(isOnGround == true)
     {
         xMovement = Input.GetAxis ("Horizontal");
         zMovement = Input.GetAxis ("Vertical");
         Vector3 distanceToMove = new Vector3(xMovement,0,zMovement);
         playerRigidbody.AddForce(distanceToMove * runningSpeed);
     }
 
     
     //Make the player face the direction he's going at
     transform.LookAt(new Vector3(xPosition+xMovement,yPosition,zPosition+zMovement));
     
     
     
     //Trigger Idle or Running Animations
     if(xMovement != 0 | zMovement != 0)
     {
         animator.SetBool("Run", true);
         animator.SetBool("Idle", false);
     }
     if((xMovement < 0.05f & xMovement > -0.05f) & (zMovement < 0.05f & zMovement > -0.05f))
     {
         animator.SetBool("Run", false);
         animator.SetBool("Idle", true);
     }            
 }
 
 
 public void OnCollisionEnter(Collision collidedOBJ)
 {
     if(collidedOBJ.gameObject.tag=="Finish")
     {
         Debug.Log(playerRigidbody.velocity);
         Debug.Log(runningSpeed);
     }
     if(collidedOBJ.gameObject.name == "Ground")
     {
         playerRigidbody.drag = 49;
         playerRigidbody.constraints &= RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezeRotationX|RigidbodyConstraints.FreezeRotationY|RigidbodyConstraints.FreezeRotationZ;
         isOnGround = true;
         canJump = true;
         runningSpeed = groundSpeed;
     
         
     }
 }
 
 
 public void OnCollisionExit(Collision exitCollidedOBJ)
 {
     if(exitCollidedOBJ.gameObject.name == "Ground")
     {
         isOnGround = false;
         
     }
 }
 
 
 

}

`

Comment
Add comment · Show 4
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 I_Am_Err00r · Aug 12, 2019 at 07:30 PM 0
Share

Honestly, that all looks really complicated to handle just a jump:

 //Jump
      if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) & isOnGround == true & canJump == true)
      {
          runningSpeed = airSpeed;
          canJump = false;
          playerRigidbody.velocity = new Vector3(x$$anonymous$$ovement,0,z$$anonymous$$ovement);
          playerRigidbody.angularVelocity = Vector3.zero;
          playerRigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
          playerRigidbody.AddForce(transform.up * jumpForce);
          playerRigidbody.drag = 0;
 
      }

I would use a premade character controller till you have a good understanding of code; character controllers are basically the most proprietary part of handling movement and knowing what state the character is in and what state they can transition to, etc. Character controllers can get really complicated really quickly as you add more features that need to check on other character states.


I'm not giving an answer because there is a lot more going on with that script you provided that needs to be addressed than just a floaty jump; Unity has character controllers you can download for free regardless if you have a first, third, or sidescrolling game; take a look at those scripts and get ideas from them:


  1. https://assetstore.unity.com/packages/essentials/asset-packs/standard-assets-32351

  2. https://assetstore.unity.com/packages/essentials/tutorial-projects/2d-game-kit-107098

  3. https://assetstore.unity.com/packages/templates/tutorials/3d-game-kit-115747


There is a reason why games like $$anonymous$$ario, Super $$anonymous$$eat Boy, and Celeste are more fun to play than a lot of other platformers, and it is because their proprietary character controller; I would use someone else's until you understand code a lot better.

avatar image Smasham I_Am_Err00r · Aug 12, 2019 at 07:45 PM 0
Share

The reason it is so complicated it because ive been trying to fix my problem for over 2h, so i got to add lot of useless stuff. And the reason why its so complicated is because i have 49 of drag but i have to remove it to jump and than change the speed (because it would be way to fast without the drag). To give you an idea, on the ground i need a 15000 speed and in the air i need a 10 speed. but if you have any idea on how to fix this well im listening to you. And by the way I just dont want to use anything that isnt made by me for this game, hope you can understand.

avatar image sacredgeometry · Aug 12, 2019 at 07:47 PM 0
Share

I can try to find your bug in a few $$anonymous$$utes but otherwise, I agree with I_Am_Error this is a bit confused. A couple of things I want to immediately mention: $$anonymous$$ethods are not things. They are actions.

... so thinking about them as in Player$$anonymous$$ovements or Camera$$anonymous$$anager you may think is trivial but its definitely contributing to your confusion.

Think about them ins$$anonymous$$d as

"$$anonymous$$ove" or "$$anonymous$$anageCamera"

i.e. doing something rather than being something.

It will also outline that your methods are perhaps far too broad. They should do one thing and and do it well.

So split your jump code out into its own method for example. Let me cook dinner then I can come and help with your bug.

avatar image Smasham sacredgeometry · Aug 12, 2019 at 07:54 PM 0
Share

Okay. Thanks for your time and your advices. I'll redo the code tonight to remove the useless stuff and ordering it.

0 Replies

· Add your reply
  • Sort: 

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

151 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

Related Questions

Limiting the top speed of a rigidbody in 3d space? 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Horizontal Drag, no Vertical Drag 1 Answer

Rigidbody loses speed 1 Answer

Rigidbody cube randomly stops moving? 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