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 CGFX360 · Feb 28, 2014 at 07:16 AM · javascriptship

Gradually increase speed

I am coding a script for a ship, and i'm not at all sure how to handle this part.

I want it so when the player tries to move forward (W key, or vertical input), the ship will gradually increase its speed to maxspeed without the need for the player to hold the button.

Pressing the S key will slowly stop the ship (again, without really needing to hold down the key) and move it in reverse if the button is held down.

Currently the script im writting is in JS.

Any advice or help would be wonderful

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

3 Replies

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

Answer by whydoidoit · Feb 28, 2014 at 07:27 AM

So what you need is a basic "State Machine" - see here for more details on what those are.

Basically the Update function of your script needs to be in one of several modes:

  • Idle - speed is tending towards 0

  • Moving - speed is tending towards maxspeed

  • Reverse - speed is tending toward max reverse speed

      enum ShipModes { Idle, Moving, Reverse };
    
        var currentMode = ShipModes.Idle;
        var currentSpeed = 0.0;
        var maxSpeed = 5.0;
        var maxReverseSpeed = -2.0;
    
    

Then in your Update function you do different things depending on the mode:

      function Update() {

           //Input phase split out - I like to do this but it's optional
           switch(currentMode) {
                 case ShipModes.Idle:
                    if(Input.GetKeyDown(KeyCode.W)) { 
                       currentMode = ShipModes.Moving;
                    }
                    if(Input.GetKeyDown(KeyCode.S)) {
                        currentMode = ShipModes.Reverse;
                    }
                    break;
                 case ShipModes.Moving:
                    if(Input.GetKeyDown(KeyCode.S)) {
                       currentMode = ShipModes.Idle;
                    }
                    break;
                  case ShipModes.Reverse:
                     if(!Input.GetKey(KeyCode.S)) {
                        currentMode = ShipModes.Idle;
                     }
                     break;
           }

           //Movement phase 
           switch(currentMode) {
                 case ShipModes.Idle:
                     currentSpeed = Mathf.Lerp(currentSpeed, 0, Time.deltaTime); //Or MoveTowards
                     break;
                 case ShipModes.Moving:
                     currentSpeed = Mathf.Lerp(currentSpeed, maxSpeed, Time.deltaTime); //Or MoveTowards
                     break;
                 case ShipModes.Reverse:
                     currentSpeed = Mathf.Lerp(currentSpeed, maxReverseSpeed, Time.deltaTime); //Or MoveTowards
                     break;
           }

           //Actually move
           transform.position += transform.forward * currentSpeed * Time.deltaTime;

      }
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 CGFX360 · Mar 01, 2014 at 02:16 AM 0
Share

This is EXACTLY what i wanted. Thank you so much! I had no idea where to begin with this, steering took me forever to figure out.

Thanks again! :D

avatar image
0

Answer by anuvk · Feb 28, 2014 at 07:22 AM

Hi,

Have you seen the Car Tutorial ? In that sample, car moves when holding down the key.

Modify the logic by checking whether 'W'/'S' key is pressed and apply Force accordingly instead of applying force on key press only. Check the js script for car in the sample.

regards,anu

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 Diori · Feb 28, 2014 at 07:28 AM

I'll write you in C#, you can apply the logic, it's just the same.

 float vx = 0f;
 float accelerationValue = 1f;
 float deccelerationValue = 1f;
 
 void Update()
 {
 
 if(W Pressed)
 {
 vx = vx + (accelerationValue * Time.DeltaTime);
 }
 
 if(S Pressed)
 {
 vx = vx - (deccelerationValue  * Time.DeltaTime);
 }
 
 shipObject.transform.position = new Vector3(shipObject.transform.position.x+vx, shipObject.transform.position.y, shipObject.transform.position.z);
 
 }
 
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 whydoidoit · Feb 28, 2014 at 07:30 AM 0
Share

That doesn't do what the OP wants. The OP wants it to increase to max speed forwards after a single press of the button.

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

23 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GetComponentInChildren(Renderer).active wont work? 2 Answers

Deriving and integer from a boolean 2 Answers


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