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 IndieScapeGames · Jul 28, 2012 at 03:52 PM · updatespeedgetkey

Getting Input and do Action Until..

Hey all,

This little problem is driving me insane. I want the user to press the Alpha-Character "1", and then while the vehicles speed is less than 3, increase the speed without having to hold down the "1" key.

I just want the user to press the key, and not have to hold it, and I can't seem to find a good example somewhere of this type of control.

Here's my code in my Update() method..

 using UnityEngine;
 using System.Collections;
 
 public class speedMod : MonoBehaviour {
  private Vector3 velocity;
  public Transform sub;
  public float subSpeed;
  public bool moveSpeed;
  
  // Use this for initialization
  void Start () {
  moveSpeed = false;
  }
  
  // Update is called once per frame
  void Update () {
  Debug.Log (moveSpeed);
  Debug.Log (subSpeed);
  if(Input.GetKeyDown(KeyCode.Alpha1)) {
  moveSpeed = true;
 
  if(moveSpeed && subSpeed<=.1) { 
  subSpeed+=0.002f * Time.deltaTime;
  } else if(subSpeed==.1){
  moveSpeed = false;
  }
  }
  
  transform.Translate(0, 0, subSpeed);
  } 
  void OnGUI() {
         GUI.Label(new Rect(10, 10, 150, 20), "Spd: "+subSpeed.ToString());
     }
 }

Edit: This is my current version of the script. Right now it's simply attached to a cube.

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

2 Replies

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

Answer by whydoidoit · Jul 28, 2012 at 04:26 PM

 var increaseSpeed = false; // script level variable

 ...


  if(Input.GetKeyDown(KeyCode.Alpha1)) increaseSpeed = true;


  if(increaseSpeed && subSpeed < .1) {
        //increase the speed
   } else {
         increaseSpeed = false;
   }
Comment
Add comment · Show 10 · 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 IndieScapeGames · Jul 31, 2012 at 03:37 PM 0
Share

Unfortunately this does not do the trick. It does for almost everything except for increasing the translation of the position.

avatar image whydoidoit · Jul 31, 2012 at 04:14 PM 0
Share

You still have a transform.Translate() after it right? Hmmm should work - wonder what I missed...

avatar image fafase · Jul 31, 2012 at 04:25 PM 0
Share

Could you post your script as it is now?

avatar image whydoidoit · Aug 01, 2012 at 01:13 AM 1
Share
 using UnityEngine;
 using System.Collections;
 
 public class speed$$anonymous$$od : $$anonymous$$onoBehaviour {
  private Vector3 velocity;
  public Transform sub;
  public float subSpeed;
  public bool moveSpeed;
  
  // Use this for initialization
  void Start () {
  moveSpeed = false;
  }
  
  // Update is called once per frame
  void Update () {
  Debug.Log (moveSpeed);
  Debug.Log (subSpeed);
  if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1)) {
       moveSpeed = true;
 }
 
  if(moveSpeed && subSpeed<=.1) { 
      subSpeed+=0.002f * Time.deltaTime;
  } else {
      moveSpeed = false;
  }
  
  
  transform.Translate(0, 0, subSpeed);
  } 
  void OnGUI() {
         GUI.Label(new Rect(10, 10, 150, 20), "Spd: "+subSpeed.ToString());
     }
 }
avatar image whydoidoit · Aug 01, 2012 at 01:15 AM 1
Share

Note how the if just controls setting moveSpeed - it does not then contain the rest of the code which is gated by that boolean set in the if. Speed will increase to 0.1 and will then stop increasing after the key is pressed. Currently there is no way to reverse this action.

Show more comments
avatar image
0

Answer by Muuskii · Sep 05, 2012 at 11:13 PM

There seems to be a lot of unneeded hassle going on here. All you need is to set a target speed right?

 using UnityEngine;
 using System.Collections;
 
 
 public class speedMod : MonoBehaviour 
 {
     private Vector3 velocity;
     public float targetSpeed;
     public float maxSpeed = 5.0f;
     public float acceleration = 1.0f;
     
     void Update()
     {

if(Input.GetKeyDown(KeyCode.BackQuote) ) targetSpeed = 0;

         for( KeyCode current = KeyCode.Alpha0; current <= KeyCode.Alpha9; current++)
         {
             if(Input.GetKeyDown(current) )
             {
                 if(current == KeyCode.Alpha0)targetSpeed = 10;        
                 else targetSpeed = current - KeyCode.Alpha0;
                 break;
             }
             
         }
         
         if( Input.GetKeyDown(KeyCode.Minus) )targetSpeed--;
         if( Input.GetKeyDown(KeyCode.Plus) )targetSpeed++;
         
         float realTargetSpeed = targetSpeed/10 * maxSpeed;
         
         //Get which way to accelerate
         float sign = Mathf.Sign(velocity.sqrMagnitude - realTargetSpeed*realTargetSpeed);
         
         if(sign != 0)
         {
             float deltaVelocity = velocity.magnitude - realTargetSpeed;
             
             if(deltaVelocity > acceleration)velocity -= velocity.normalized * acceleration * sign;
             else velocity = velocity.normalized * realTargetSpeed;                        
         }
         
         //put your method of movement here
         move(velocity);
         
     }
     
 }

This should make your velocity between 0 - maxspeed

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

8 People are following this question.

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

Related Questions

update() too fast? 2 Answers

How to increase the speed of the character in OnTriggerEnter function? 1 Answer

Increase Players Speed Overtime in Endless Runner? Is my Code Unoptimized? 0 Answers

Laggy camera on const moving speed 1 Answer

Audio playing problem 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