• 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
1
Question by Hondune · Oct 10, 2010 at 11:28 PM · speedaccelerationfriction

gradually moving an object up to speed rather then instantly?

i want to know how to, on button press, make an object move without instantly getting to the speed i set it to move at. right now im using this script

function Update () {
if (Input.GetKey ("left"))
    transform.position.x = transform.position.x + -0.10;
if (Input.GetKey ("right"))
    transform.position.x = transform.position.x + 0.10;

}

which works, but when the button is pressed the object instantly moves at 0.10, i would like it to accelerate to 0.10 then keep moving at that, then when the button is released de-accelerate back to not moving. i hope this makes sense :/

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

7 Replies

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

Answer by IJM · Oct 11, 2010 at 01:20 AM

This code will work for you:

var Speed : float = 0;//Don't touch this var MaxSpeed : float = 10;//This is the maximum speed that the object will achieve var Acceleration : float = 10;//How fast will object reach a maximum speed var Deceleration : float = 10;//How fast will object reach a speed of 0

function Update () { if ((Input.GetKey ("left"))&&(Speed < MaxSpeed)) Speed = Speed - Acceleration Time.deltaTime;
else if ((Input.GetKey ("right"))&&(Speed > -MaxSpeed)) Speed = Speed + Acceleration
Time.deltaTime; else { if(Speed > Deceleration Time.deltaTime) Speed = Speed - Deceleration Time.deltaTime; else if(Speed < -Deceleration Time.deltaTime) Speed = Speed + Deceleration Time.deltaTime; else Speed = 0; }

transform.position.x = transform.position.x + Speed * Time.deltaTime; }

Comment
Add comment · Show 7 · 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 Hondune · Oct 11, 2010 at 03:46 AM 0
Share

worked like a charm! thank you both very much :)

avatar image IJM · Oct 11, 2010 at 04:44 AM 0
Share

No problem mate ;)

avatar image FreeTimeDev · Oct 12, 2010 at 09:58 AM 0
Share

$$anonymous$$ark this answer as correct!

avatar image santosh810666 · Nov 11, 2013 at 07:21 AM 0
Share

hi your code is awesome i am trying to maintain $$anonymous$$imum speed but when i increase speed it is floating to 0 and it is increasing again can you please help me

avatar image aadithyabk · Oct 20, 2014 at 08:23 AM 0
Share

Can anybody please explain the logic behind this ? Why is Speed = Speed - Acceleration Time.DeltaTime ?

What is operator between Accleration and Time.DeltaTime ? Please help

avatar image PanzerPotato aadithyabk · Jun 16, 2018 at 03:02 PM 0
Share

It should be Speed = Speed - (Acceleration * Time.DeltaTime).

Show more comments
avatar image
2

Answer by dreg_master · Jan 12, 2017 at 10:38 AM

@IJM

Dude, you are a legend sir. I spend Days trying to get this to work with a script 3 pages long and still pulling out hair. Your solution was in Java i converted into c# and it worked marvelously. Thank you. here is what worked for me in C#

using UnityEngine; using System.Collections;

public class test2 : MonoBehaviour { public Vector3 position; float Speed = 0; //Don't touch this float MaxSpeed = 10; //This is the maximum speed that the object will achieve float Acceleration = 10; //How fast will object reach a maximum speed float Deceleration = 10; //How fast will object reach a speed of 0 // Use this for initialization void Start() { transform.position = position; }

 // Update is called once per frame
 void Update()
 {

     if ((Input.GetKey("left")) && (Speed < MaxSpeed)) Speed = Speed - Acceleration * Time.deltaTime;
     else if ((Input.GetKey("right")) && (Speed > -MaxSpeed)) Speed = Speed + Acceleration * Time.deltaTime;
     else
     {
         if (Speed > Deceleration * Time.deltaTime) Speed = Speed - Deceleration * Time.deltaTime;
         else if (Speed < -Deceleration * Time.deltaTime) Speed = Speed + Deceleration * Time.deltaTime;
         else
             Speed = 0;
     }
     position.x = transform.position.x + Speed * Time.deltaTime;
     transform.position = position;

 }

}

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
1

Answer by Ray-Pendergraph · Oct 11, 2010 at 12:57 AM

Try lerping it, here is the documentation:

transform.position = Vector3.Lerp(start.position, end.position, Time.time);

This gradually moves between two points. The movement is constant over time, to have the movement gradual (like building momentum) at the start and stop points I believe you must call lerp with the current position like so:

transform.position = Vector3.Lerp(transform.position, end.position, Time.time);

Yes, actually just look at the second example on that link. :-). Duh.

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
1

Answer by ChimeraPriest · Nov 11, 2015 at 01:16 AM

For 2D platformer movement with some acceleration and deceleration...

  void Update()
     {
         float h = Input.GetAxis("Horizontal");

         Rigidbody2D rbody = GetComponent<Rigidbody2D>();

           rbody.velocity = Vector3.right * h * speed; 
     }

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
1

Answer by tjBlueBeast · Dec 09, 2015 at 09:48 AM

 public float speed = 0f; 
 public float maxSpeed = 10f;
 public float forceMagnitude = 2f;
 
 void FixedUpdate() {
         // Store the input axes.
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
 
         // Move the player around the scene.
         Move (h, v);
 
         //other stuff...
     }
 
 void Move(float h, float v) {
         if (v != 0 || h != 0) {
             // Set the movement vector based on the axis input.
             movement.Set (h, 0f, v);
             speed = Mathf.Min(speed + forceMagnitude * Time.deltaTime, maxSpeed);
         } else {
             speed = Mathf.Max(speed - forceMagnitude * Time.deltaTime * 1.5f, 0);
         }
         //Debug.Log (speed);
 
         // Normalise the movement vector and make it proportional to the speed per second.
         movement = movement.normalized * speed * Time.deltaTime;
         // Move the player to it's current position plus the movement.
         playerRigidbody.MovePosition(transform.position + movement);
     }

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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Going Down and Increasing speed 1 Answer

How to control acceleration 3 Answers

Acceleration with CC 0 Answers

Where can I find details on the physics engine? 2 Answers

How to increase the acceleration of a falling object? 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