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 prithul · Feb 25, 2015 at 06:15 AM · physicsrigidbodyvelocity

Setting rigidbody.velocity giving unexpected result.

I am trying to implement a 2.5D platformer script:

  1. I am clamping the rigidbody's velocity.x between a certain value so that it just doesn't accelerate for ever . In this case it is from 0.0f to 0.8f units.

  2. adding a force to move the player.

  3. All this are being done in FixedUpdate, which is set to a FixedDelta of .01f in projects settings.

  4. Jumping is done by setting velocity.y directly.

  5. Drag is set to 0.0f

The problem is, clamping the velocity causes the character to lose it's momentum. So to fix it I am storing the velocity.x component from the time of jump and setting it directly while player is in air. Now, it works all fine except that I need to scale the velocity.x component by some value ( 8.0 in my particular case) to make the player have momentum in air. If I set it to my maximum move speed ( 0.8f in my case) it almost doesn't move while in air. But when grounded, it moves normally by setting that value directly.

Now the problem isn't that the velocity.x value isn't assigned correctly. Because I have checked many times (by Debug.Log as well as printing to guiText) that the velocity.x of the character, while it is in air, is exactly what it should be. But for some reason it is not moving in that speed along the X axis.

Relevant code: [ temVelocity is restored and assign`enter code here`ed in the beginning and end of FixedUpdate respectavily ]

 void FixedUpdate()
 {


     temVelocity = rigidbody.velocity;   // get ref for future use

     /////////////////// JUMP TIMER ///////////////////////
     //update timer in fixed update to make jump deterministic
     if (onAir)
         jumpTimer.Update(Time.fixedDeltaTime);



     //check if jump time remaining
     if (jumpTimer.isOver())
     {
         canJump = false;
     }
     //////////////////    END    ///////////////////////





     ///////////////  MOVEMENT TIMER ////////////////////
     //movement stopped so reset movement timer
     if (Input.GetAxisRaw("Horizontal") == 0)
     {
         movementTimer.reset();
         movementTimer.Target = int.MaxValue;
     }

     //player moving , so update movement timer
     if(Input.GetAxisRaw("Horizontal")!=0)
     {
         movementTimer.Update(Time.fixedDeltaTime);
     }
     //////////////////////  END  //////////////////////////




     //when on ground and moving
     if (!onAir && Input.GetAxisRaw("Horizontal") != 0)
     {
         dir = (int)Input.GetAxisRaw("Horizontal");  // get face direction
         rigidbody.AddForce(Input.GetAxis("Horizontal") * transform.right * moveForce, ForceMode.Force);

         //movement timer functions
         movementTimer.Update(Time.fixedDeltaTime);

     }

     if(onAir)
     {
         //released movement while in air; should start to loose momentum
         if(Input.GetAxisRaw("Horizontal")==0)
         {
             loseMomentum = true;
         }
         

         //momentum losing process
         if(loseMomentum)
         {
             releaseSpeedX -= dampingFactor;
             releaseSpeedX = Mathf.Clamp(releaseSpeedX, 0, int.MaxValue);
         
         }

         //set momentum of player
         temVelocity.x = releaseSpeedX*8f * dir;        //dir has been stored while on ground and never changed in air
     
     }

     //clamp speed whlie grounded
     if(!onAir && Mathf.Abs(rigidbody.velocity.x)>speedLimit )
     {
         temVelocity.x = speedLimit * Mathf.Sign(temVelocity.x) ;
     }
     



     //when player can jump and is trying to jump
     if (canJump && Input.GetAxisRaw("Vertical") == 1)
     {
         temVelocity.y = jumpSpeed;
         
         //store value to use as momentum at the moment of jump; interpolate between min and max possible value 
         if (!jumped)
         {
             if (Input.GetAxisRaw("Horizontal") != 0)
             {
                 float temDelay = Mathf.Clamp(movementTimer.Time, 0, 1f);
                 releaseSpeedX = Mathf.Lerp(speedLimit / 2f, speedLimit, temDelay);

                 loseMomentum = false;   //retaining momentum        
                 jumped = true;
             }
             else              // no direction chosen, jump upwards
             {
                 releaseSpeedX = 0f;
                 jumped = true;
                 loseMomentum = false;
             }
         }
     }



     //text.GetComponent<GUIText>().text = rigidbody.velocity.x +" "+ releaseSpeedX;
     
     //player on ground and is NOT moving, stopping flag allows not to be constantly applying stopping velocity
     if(stopping && !onAir  &&  Input.GetAxisRaw("Horizontal")==0)
     {
         stopping = false;
         text.GetComponent<GUIText>().text = "yes";
         temVelocity.x = 0;// * Mathf.Sign(tem.x);

     }
     else if(Input.GetAxisRaw("Horizontal") !=0)
     {
         stopping = true;
     }


     rigidbody.velocity = temVelocity;   //reassign velocity
 }




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

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

2 People are following this question.

avatar image avatar image

Related Questions

How do I reduce the bounciness of a ball affected by gravity in .js? 2 Answers

Throwing objects with a fixed joint? 1 Answer

Move Ball With Same Velocity on Slope 1 Answer

Rigidbody velocity and velocity.magnitude always return 0 1 Answer

How can I convert velocity/direction to Force? 3 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