• 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 timdouglas90 · Jun 02, 2014 at 10:11 PM · move speed

'w' to modify speed

Hi, I'm very new to this, and would like a hand with this... using UnityEngine; using System.Collections;

 public class DragonPilot : MonoBehaviour {
     public float speed = 90.0f;
     public float dragonTopSpeed = 15;
     public float RevdragonTopSpeed = -10;
     public float dragonAcceleration = 5;
     public float dragonDecceleration = 1.5f;
     public float NewSpeed;
 
     // Use this for initialization
     void Start () {
         Debug.Log ("dragonpilot script added to: " + gameObject.name);
         //Hide cursor
         Screen.lockCursor = true;
     }
     
     // Update is called once per frame
     void Update () {
         
         transform.position += transform.forward * Time.deltaTime * speed;        ///Set speed///
         
         speed -= transform.forward.y * Time.deltaTime * 50.0f;
         
         if (speed < 35.0f) {
             speed = 35.0f;
         }
         if(Input.GetButton('w') && (speed < dragonTopSpeed )) {
             NewSpeed = speed + dragonAcceleration;
             speed = NewSpeed;
         }
         
         if (Input.GetButton('s') && (speed > RevdragonTopSpeed )){
             NewSpeed = speed - dragonDecceleration;
             speed = NewSpeed;
         }
         
         if (speed > 0){
             transform.Translate(Vector3.forward * Time.deltaTime * speed );
             
         }
         if (speed < 0) {
             transform.Translate(Vector3.forward * Time.deltaTime * speed );
         }
     }
         
         transform.Rotate( Input.GetAxis ("Mouse Y" ) * 1.0f, Input.GetAxis ("Horizontal")  * 0.5f , -Input.GetAxis ("Mouse X") * 1.0f );
         
         
         ///Flight controls: Mouse pitch, KB Yaw, Mouse roll///
         


I need 'w' to increase speed, but speed is being affected by pitch too...how can I achieve this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Jun 02, 2014 at 11:53 PM

If you want 'w' to increase speed gradually while it's held down: just increase speed by a little bit while it's held down.

 if (Input.GetKey(KeyCode.W)) {
     speed += Time.deltaTime;
 }

If you want 'w' to increase speed dramatically while it's held down: declare some temporary "real speed" variable, which is normally just speed, but can be multiplied by 2 when 'w' is held.

 float realSpeed = speed;
 if (Input.GetKey(KeyCode.W)) {
     realSpeed *= 2;
 }

How to check if 'w' is down? With the Input class, of course! :)

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 timdouglas90 · Jun 04, 2014 at 10:29 PM 0
Share

Ah thanks man! I did it a similar way but now I am trying to get $$anonymous$$ch and Roll to be affected by speed...

 using UnityEngine;
 using System.Collections;
 
 public class DragonPilot : $$anonymous$$onoBehaviour {
     public float speed = 90.0f;
     public float boost = 200f;
     public float $$anonymous$$ = 35.0f;
     public float max = 150.0f;
 
 
 
     // Use this for initialization
     void Start () {
         Debug.Log ("dragonpilot script added to: " + gameObject.name);
         Screen.lockCursor = true;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         transform.position += transform.forward * Time.deltaTime * speed;///Set speed///
 
         //increase speed when 'w' is pressed by 1
         if (Input.Get$$anonymous$$ey("w"))
         {
             if(speed <= max)
                 speed += 1;
             else
                 speed = max;
         }
 
         if (Input.Get$$anonymous$$ey("space")) //boooooost
         {
             transform.position += transform.forward * 40 * Time.deltaTime;
         }
                               
            
 
         //decrease speed with 's', there's a problem with this.
         else if(Input.Get$$anonymous$$ey("s"))
         {
             if(speed >= 35)
                 speed -= 1;
         }
 
         //lower move speed when 'w' is let go
         else if(!Input.Get$$anonymous$$ey("w"))
         {
             if(speed >= $$anonymous$$)
                 speed -= 0.5f;
         }
 
         //lower move speed when 's' is let go
         if(!Input.Get$$anonymous$$ey ("s"))
         {
             if(speed <= 90.0f)
                 speed += 0.5f;
         }
     
         transform.Translate(Vector3.forward * Time.deltaTime * speed / 10);
         
 
 
         if (speed > 150.0f) {
             speed = 150.0f;    //max speed
         }
     
 
 
         speed -= transform.forward.y * Time.deltaTime * 10.0f;
 
         if (speed < 35.0f) {
             speed = 35.0f;
         }
 
 
 
         transform.Rotate( Input.GetAxis ("$$anonymous$$ouse Y" ) * 2.0f, Input.GetAxis ("Horizontal")  * 0.5f , -Input.GetAxis ("$$anonymous$$ouse X") * 2.0f );  ///Flight controls: $$anonymous$$ouse pitch, $$anonymous$$B Yaw, $$anonymous$$ouse roll///
 
         float terrainHeightWhereWeAre = Terrain.activeTerrain.SampleHeight( transform.position );  ///Samples terrain height relative to position
 
         if (terrainHeightWhereWeAre > transform.position.y) {                    ////Detect ground collision////
              transform.position = new Vector3(transform.position.x,
                                              terrainHeightWhereWeAre,
                                              transform.position.z);
         }
     }
 }



this is what I have so far..I'm assu$$anonymous$$g I can create a new variable that decreases the floats...

 transform.Rotate( Input.GetAxis ("$$anonymous$$ouse Y" ) * 2.0f, Input.GetAxis ("Horizontal")  * 0.5f , -Input.GetAxis ("$$anonymous$$ouse X") * 2.0f );  ///Flight controls: $$anonymous$$ouse pitch, $$anonymous$$B Yaw, $$anonymous$$ouse roll///

??

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

22 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

Related Questions

How can I randomize the speed of enemy movement? 2 Answers

Move object with max speed and stop 1 Answer

Collide with a 'switch' gameobject that lowers the speed of another gameobject 1 Answer

Rididbody movement at the end of a rotating platform 0 Answers

C# trying to move a object 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