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 Kciwsolb · Jun 07, 2018 at 06:30 PM · physicsrigidbodyvelocityplatforms

Calculate player velocity change while taking into account velocity of what the player is standing on

I have a Rigidbody character who is controlled by adding force as a velocity change. I already have it set up so that external forces can still be applied (if I wanted to add an explosion force near the character, the character would respond naturally).

However, I decided to try to add a moving platform to see how it responded. I realized this did not work well with the current system. My platforms have a kinematic Rigidbody. I am moving them using Rigidbody.MovePosition with interpolation. This works fine for normal Rigidbody objects (a cube with Rigidbody and Box Collider stays on the platform just fine). The reason it is not working for the character seems to be due to setting the velocity as my means for moving the player. So, what I am trying to wrap my head around is a way to compensate for the ground’s velocity before setting my character’s velocity.

This is really more of a "because I can" thing. I would normally just parent the character to the platform, and call it a day. But I am interested in making this work...

This movement code is on the character and called every FixedUpdate:

 //Calculate force to add:
 float velocity;
 if(crouching) { velocity = crouchVelocity; }
 else if(running) { velocity = runVelocity; }
 else { velocity = walkVelocity; }

 float vertical = Input.GetAxis("Vertical");
 float horizontal = Input.GetAxis("Horizontal");

 Vector3 targetVelocity = (forward * vertical + right * horizontal) * velocity; //forward and right are custom Vector3s that  mirror the right and forward of my camera
 Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
 Vector3 velocityChange = transform.InverseTransformDirection(targetVelocity) - localVelocity;

 velocityChange = Vector3.ClampMagnitude(velocityChange, !jumping ? groundControl : airControl);
 velocityChange.y = !jumping ? 0.0f : -localVelocity.y + jumpVelocity;
 velocityChange = transform.TransformDirection(velocityChange);

 //Apply force:
 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

What I want to add is a way to calculate my velocityChange while taking into account the velocity of the ground I am standing on. That way I can still move and jump.

I naively tried adding this right before my AddForce:

     GameObject ground = GetGround(); //Get ground checks for and returns ground if it is there
     if(ground)
     {
         Rigidbody groundRigidbody = ground.GetComponent<Rigidbody>();
         if(groundRigidbody)
         {
             velocityChange += groundRigidbody.velocity;
         }
     }

But of course, that resulted in my character accelerating off the platform since the next FixedUpdate the player would be adding in the velocity of the platform again even though he is already moving that fast.

I also tried to get the difference between the platforms velocity and the characters velocity and add that into velocity change, but that didn't seem to have the desired effect either. The character kind of kept up with the platform, but lagged behind and eventually fell off.

If I could get this working, it would mean my character could stand and move on any moving Rigidbody object. I just think that would be neat, albeit unrealistic in some (okay many) cases.

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

160 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

Related Questions

Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers

Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers

Inconsistent Rigidbody2D velocity. 0 Answers

What is the velocity on a given axis of a rigidbody? 2 Answers

GameObject disappears when velocity is set to (near) zero. 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