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 DeveLOLper · Aug 13, 2013 at 07:25 PM · movement scriptfunction updatebasic programminganother script

Acceleration script

How do I change this script so that it has simple acceleration? I you have a few extra minutes, it'd be nice to explain. :)

 //Basic force to move a rigidbody object
 var power : float = 500.0;
  
 function Update () {
  if(Input.GetKeyDown(UpArrow)){
  rigidbody.AddForce(Vector3(0,0,power));
 }
 
Comment
Add comment · Show 2
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 Raph3004 · Aug 13, 2013 at 07:27 PM 0
Share

What do you mean by 'simple acceleration'?

avatar image DeveLOLper · Aug 13, 2013 at 09:00 PM 0
Share

I mean nothing too advanced

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by asafsitner · Aug 13, 2013 at 09:45 PM

From **Wikipedia**, acceleration means that an object moves progressively faster.
This means that instead of feeding an object a fixed velocity, you increase the velocity over time. For example:

 //Basic acceleration
 var velocity : float = 10.0;
 var acceleration : float = 10.0;
  
 function Update () 
 {
     //this line adds the acceleration value to the power value over the course of 1 second, up to infinity
     power = power + (Time.deltaTime * acceleration);

     //this line feeds the ever-growing force to the object, propelling it faster and faster over time
     transform.Translate(0, 0, power);
 }

You can do something a little more advanced, such as declaring an initial velocity and a final (terminal) velocity and even a deceleration rate.

This is a little more complex example:

 //Acceleration example, Level 2
 
 //this is our target velocity while decelerating
 var initialVelocity : float = 0.0;
 
 //this is our target velocity while accelerating
 var finalVelocity : float = 500.0;
 
 //this is our current velocity
 var currentVelocity : float = 0.0;
 
 //this is the velocity we add each second while accelerating
 var accelerationRate : float = 10.0;
 
 //this is the velocity we subtract each second while decelerating
 var decelerationRate : float = 50.0;
 
 //movement happens here
 function Update ()
 {
     if(Input.GetKeyDown(UpArrow))
     {
         //add to the current velocity according while accelerating
         currentVelocity = currentVelocity + (accelerationRate * Time.deltaTime);
     }
     else
     {
         //subtract from the current velocity while decelerating
         currentVelocity = currentVelocity - (decelerationRate * Time.deltaTime);
     }
 
     //ensure the velocity never goes out of the initial/final boundaries
     currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
 
     //propel the object forward
     transform.Translate(0, 0, power));
 }

And so on.

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 PointyPigheadGames · Oct 13, 2016 at 05:05 AM

Here's the script I use:

  //Acceleration example, Level 2
  
  //this is our target velocity while decelerating
  var initialVelocity : float = 0.0;
  
  //this is our target velocity while accelerating
  var finalVelocity : float = 500.0;
  
  //this is our current velocity
  var currentVelocity : float = 0.0;
  
  //this is the velocity we add each second while accelerating
  var accelerationRate : float = 10.0;
  
  //this is the velocity we subtract each second while decelerating
  var decelerationRate : float = 50.0;
  
  //movement happens here
  function Update ()
  {
      if(Input.GetKey(KeyCode.Space))
      {
          //add to the current velocity according while accelerating
          currentVelocity = currentVelocity + (accelerationRate * Time.deltaTime);
          transform.Translate(0, 0, currentVelocity);
      }
      else
      {
          //subtract from the current velocity while decelerating
          currentVelocity = currentVelocity - (decelerationRate * Time.deltaTime);
          if(currentVelocity > 0) {
              transform.Translate(0, 0, currentVelocity);
          }
          else {
              transform.Translate(0, 0, 0);
          }
      }
  
      //ensure the velocity never goes out of the initial/final boundaries
      currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
  
      //propel the object forward
      
  }

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

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Help on a basic script 2 Answers

Help for basic board game but for me it's hard.. 0 Answers

How to access variable from another function? 2 Answers

UnityEngine.Input.GetMouseButton(1)) issue 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