• 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
Question by Scotty_P · Nov 06, 2014 at 07:20 AM · c#timespeeddelay

how to slow down speed increase?

I am using this script on a space ship, the negative values make the ship go in reverse. it works exactly as I want it to, except I want the speed to increase more slowly, I would like the speed to increase by 10 units a second. can someone give me some tips as to how I would achieve this

 using UnityEngine;

using System.Collections;

public class SpeedControl : MonoBehaviour {

 [SerializeField]
 float speed = 2.0f;

 public float trueSpeed = 0.0f;
 public static string speedDisplay;
 // Use this for initialization
 void Start () {

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

     float power = Input.GetAxis("Power");

             if (trueSpeed < 100 && trueSpeed > -30){
         trueSpeed += power;
     }
     if (trueSpeed > 100){
         trueSpeed = 99.99f;    
     }
     if (trueSpeed < -30){
         trueSpeed = -29.99f;    
     }
     if (Input.GetKey("backspace")){
         trueSpeed = 0;

     }
     speedDisplay = trueSpeed.ToString("0.0");

     rigidbody.AddRelativeForce(0,0,trueSpeed*speed*Time.deltaTime);
 }

 }
Comment

People who like this

0 Show 1
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 Scotty_P · Nov 06, 2014 at 05:34 AM 0
Share

sorry for being vague, im a bit new to all this, I have updated my original question with a more full question and the entire script

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by slay_mithos · Nov 06, 2014 at 07:57 AM

If you want the acceleration to be slower, but keeping the min/max speed, changing: float power = Input.GetAxis("Power");

into: float power = Input.GetAxis("Power")*0.5f;

Should half your speed increase.

This does not increase by 10 per second, because the whole point of the script is to handle the stick from a gamepad (the more you push it, the more it accelerates).

For a flat increase, you would want your "power" to use a hard number.

For a stable 10 per second, you would need to multiply 10 with Time.deltaTime (see http://docs.unity3d.com/ScriptReference/Time-deltaTime.html for an example)

Comment

People who like this

0 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 Scotty_P · Nov 06, 2014 at 08:05 AM 0
Share

no, that will work fine for what I need, thank you very much

avatar image

Answer by b1gry4n · Nov 06, 2014 at 08:24 AM

Alternatively you could utilize : http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html

     float velocity = 1.0f;
     float currentSpeed = 0.0f;
     float maxSpeed = 100.0f;
     float dampen = 25.0f;
     void Update () {
         currentSpeed = Mathf.SmoothDamp(currentSpeed, maxSpeed, ref velocity, Time.deltaTime * dampen);
         Debug.Log(currentSpeed);
     }

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Calculating animation clip length with speed modifier 1 Answer

Have a delay after each jump, so user cant spam jump 3 Answers

Accelerate/Decelerate Custom Game Clock 2 Answers

Do something every 0.5 seconds 3 Answers

Multiple Cars not working 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