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 Trekopep · Aug 19, 2018 at 12:24 AM · rigidbody.velocityskateboardvector3.project

Skateboard Movement / Setting X Velocity Axis to 0

Background: This is a skateboarding game. The current setup uses Rigidbody.AddForce for acceleration, Transform.Rotate for turning, and sets Rigidbody.velocity directly in certain other places. Let's assume the skateboard can only move in a forward direction.

I want to be able to decide how much turning reduces forward momentum, ranging from keeping only your initial movement vector projected in the direction of the board to keeping all of your forward momentum.
The first case seems like it should be simple, but I'm having difficulty. In this case, moving forward and then turning 90° should result in the skateboard coming to a complete stop (there may also be a conceptual problem here, but I'm less concerned about that).

Here's the code thus far (simplified for clarity), with two different versions I've tried:

 void FixedUpdate () {
     Rigidbody rb = GetComponent<Rigidbody>();
 
     transform.Rotate(Vector3.up, horizontalAxis * groundedTurnSpeed * Time.deltaTime);
     rb.AddRelativeForce(Vector3.forward * verticalAxis * accelerationSpeed);
 
     // Version 1
     // Change the velocity from global to local coordinates
     newVelocity = transform.InverseTransformVector(rb.velocity);
     // Get rid of the velocity that isn't along the forward axis of the skateboard
     newVelocity.x = 0;
     // Convert back to global coordinates
     rb.velocity = transform.TransformVector(newVelocity);
 
     // Version 2
     rb.velocity = Vector3.ProjectOnPlane(rb.velocity, transform.right);
 }

Both of these versions successfully remove all of the movement in the player's x direction, as expected, but the player does not slow down as quickly as expected. Instead of coming to a stop after turning 90 degrees (since that's perpendicular to the initial direction of movement), the player can make several full turns and still have some forward momentum remaining.


My only guesses here are that FixedUpdate is such a small timestep that it's not actually removing a measurable momentum every frame, or that some other underlying system is tripping me up.

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rainChu · Aug 19, 2018 at 12:39 AM

When you turn, does your character snap 90 degrees in a single frame? If not, I would expect this behavior. Each FixedUpdate, the angle now considered "forward" changes. In real life, a skateboarder can also apply acceleration, then turn several times without applying more before coming to a stop.


If you specifically want the behavior where a gradual turn of 90 degrees fully stops you, you should instead hold onto a forward velocity stored as a vector each time the player applies acceleration, and every FixedUpdate, manually set the rigidbody to that, projected onto the current forward.

Comment
Add comment · Show 1 · 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 Trekopep · Aug 19, 2018 at 01:03 AM 0
Share

Hm. Thinking more about it (despite all my thinking about it before), you're completely correct. Taking a look at the simplest case of this, turning 45 degrees, the speed would be cut in half, and then turning 45 degrees again, the speed would be cut in half again. That said, using projection or setting x to 0 is obviously dependent on the number of steps, and therefore isn't consistent over turn amount.

I'm not a fan of your first suggestion, given that the act of turning should slow the player down, and in your solution the player could turn back toward their initial rotation to speed up again. That said, I'm not positive what the best solution would be. $$anonymous$$aybe as simple as slowing the player by a certain amount or percentage of their speed based on their turn amount? Ultimately, it probably just boils down to whatever "feels" best at this point, now that I'm certain that the way I was doing it wasn't the right way to go about it.

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

88 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

Related Questions

Rigidbody isn't working properly 1 Answer

Problem with a ballistic trajectory simulation (rigidbody.velocity) 0 Answers

When stopping Rigidbody.velocity 1 frame stutter C# 0 Answers

Is there any way to increase a triggers check rate? 2 Answers

How to get vector3.zero when using rigidbody.Addforce? 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