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 1337keegan · Nov 29, 2018 at 10:40 PM · controllervelocitymovement scriptplayer movementacceleration

(C#) Accelerating Player Based On Distance

Hello, I've been stuck working on this code for a few days now and I've made some progress but am totally stumped.

The scenario: A direction is pressed and the player accelerates linearly from 0 to a speed of lets say 3u/s and hits that speed within 0.5 units distance. The time it takes to accelerate is irrelevant.

I understand that the first thing i have to do is calculate the acceleration, which i've found multiple contradicting formulas for, and then increment the player's position based on that acceleration. Would I also have to calculate how long that would take in order to move the player properly? I've looked extensively into lerps and simply can't figure out a solution. Any help is welcome at this point. Thank you!

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 Eno-Khaon · Nov 30, 2018 at 10:54 AM

This is where a good reference point can really come in handy.

By a typical acceleration formula, you're taking the current speed, the target speed, and the time spent accelerating between those two speeds. With that information, you can determine the distance traveled and the rate of acceleration to reach that point.

This is done using the formulas:

tgtSpeed-curSpeed/time = acceleration

(avgSpeed/2)*time = distance


Since you know the distance, however, let's take this equation and work backwards, shall we?

distance/avgSpeed = time


Yep, time still winds up being a valuable factor in this. But since the time can be determined by reversing the speed-to-distance equation, you can use that to turn it back around and calculate the acceleration rate (force) to apply to reach the desired speed.
 public static float AccelerateToTarget(float currentSpeed, float targetSpeed, float distance)
 {
     float averageSpeed = (currentSpeed + targetSpeed) * 0.5f;
     float time = distance / averageSpeed;
 
     float speedDifference = targetSpeed - currentSpeed;
     float acceleration = speedDifference / time;
     return acceleration;
 }

 float forceToAdd;

 void Start()
 {
     Rigidbody rb = GetComponent<Rigidbody>();
     // Example call using your values
     forceToAdd = AccelerateToTarget(rb.velocity.magnitude, 3.0f, 0.5f);
 }

 void FixedUpdate()
 {
     rb.AddForce(transform.forward * forceToAdd);
 }


Granted, this example won't stop accelerating after it reaches that speed after that time, but it will reach the target speed of 3u/s upon traveling 0.5 units (not counting applicable approximation errors and friction).

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 1337keegan · Nov 30, 2018 at 04:37 PM

That's excellent, thank you! However is there a way to do this positionally rather than through force? my object isn't a rigidbody, it's a 2D sprite and I would like to manipulate it through transform.position if at all possible.

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 Eno-Khaon · Nov 30, 2018 at 07:52 PM 0
Share

Position is calculated by the physics engine(s) about like this:

 // Written on a 2D basis
 void FixedUpdate()
 {
     Vector2 velocityPerFrame = lastFrameVelocity + (acceleration * Time.fixedDeltaTime);
     Vector2 positionPerFrame += velocityPerFrame * Time.fixedDeltaTime;
 }


With that in $$anonymous$$d, that's also the general means of replacement. The AccelerateToTarget() example I gave (when multiplied by a normalized movement Vector) would be the force to add per frame (multiplied by Time.deltaTime or the like) to the current velocity.

You may also want to take a look at the answer @Bunny83 gave to this question to help smooth out the acceleration applied.

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

102 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

Related Questions

Help with my Dash move? 1 Answer

Slow falling when using Rigidbody set velocity as playercontroller 0 Answers

Character accelerates while running towards walls. "Sonic effect" 1 Answer

How to stop player movements while the player is falling? 0 Answers

My player velocity changing by itself? 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