Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ThatGuyBrian · Oct 17, 2016 at 01:25 AM · c#speedcontrols

Setting up a gear system of sorts...

I really hate being a burden, especially when I don't know what exactly to do in this case, but I'm not sure what to do here. In brief, what I'm trying to do is alter my control script (which is already a mess of code snippets taken from various online sources) to allow the player to select about 6 speeds (Reverse, Stop, 1/4, 1/2, 3/4, and Full.) while having to only press the button once. (Bridge Commander's control scheme is a perfect example of this.) So far I've managed to program the exact speeds that I want and I've successfully bound them to the buttons, but I have no idea how to maintain those speeds when I let go.

      public float xFactor = 70f;
     public float yFactor = 150f;
     public float zFactor = 3f;
 
     public float AmbientSpeed = 100.0f;
 
     public float RotationSpeed = 200.0f;
 
     void Update()
     {
         var x = Input.GetAxis("Horizontal") * Time.deltaTime * xFactor;
         var z = 0.5f;
         var y = Input.GetAxis("Vertical") * Time.deltaTime * yFactor;
 
         if (Input.GetKey(KeyCode.Keypad0))
             z = Time.deltaTime * 0;
         if (Input.GetKey(KeyCode.Keypad1))
             z = Time.deltaTime * 50;
         if (Input.GetKey(KeyCode.Keypad2))
             z = Time.deltaTime * 100;
         if (Input.GetKey(KeyCode.Keypad3))
             z = Time.deltaTime * 150;
         if (Input.GetKey(KeyCode.Keypad4))
             z = Time.deltaTime * 200;
         if (Input.GetKey(KeyCode.R))
             z = Time.deltaTime * -50;
 
 
         transform.Rotate(y, 0, 0);
         transform.Translate(0, 0, z);
         transform.Rotate(0, x, 0);
         Quaternion AddRot = Quaternion.identity;
         float roll = 0;
         float pitch = 0;
         float yaw = 0;
         roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
         pitch = Input.GetAxis("Pitch") * (Time.deltaTime * RotationSpeed);
         yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);
         AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
         GetComponent<Rigidbody>().rotation *= AddRot;
         Vector3 AddPos = Vector3.forward;
     }

Can anybody lend me a hand here?

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
0
Best Answer

Answer by b1gry4n · Oct 17, 2016 at 01:52 AM

you dont need to multiply your x or y values by time.deltatime. GetAxis is going to return a value between -1 and 1. -1 being left/down, 0 being no input, and 1 being right/up. Multiplying it by Time.deltaTime (which will be some decimal value like 0.3235235 or something) does nothing at this point other than make control harder. You only need to multiply the very last step by Time.deltaTime, being the actual function to rotate or move the object.

You need to store the selected speed. First off...use "GetKeyUp" or GetKeyDown"...The reason being "GetKey" requires constant pressing for it to be "true". up or down fires when the button is either up or down. The "Z" value, make it a variable.

 public float speed;

Now when the button is pressed, change the speed variable to your desired speed.

          if (Input.GetKeyDown(KeyCode.Keypad4))
              speed = 200;

and finally, where you are actually applying the speed (take note, i didnt multiply by Time.deltaTime in the key press)

    transform.Translate(0, 0, speed * Time.deltaTime);

Comment
Add comment · Show 3 · 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 ThatGuyBrian · Oct 17, 2016 at 01:15 PM 0
Share

Alright, that didn't quite help. It's still not moving at the set speeds whenever I let go. Did I implement your fixes incorrectly? Z is already a variable, and I got rid of the deltaTimes.

avatar image b1gry4n ThatGuyBrian · Oct 17, 2016 at 01:54 PM 0
Share

z is not already a stored variable. You are declaring it every frame at the top of update. The following frame after you let go of any button, z is declared new and reset to 0.5

avatar image b1gry4n b1gry4n · Oct 17, 2016 at 02:00 PM 0
Share
     public float xFactor = 70f;
     public float yFactor = 150f;
     public float zFactor = 3f;
 
     public float AmbientSpeed = 100.0f;
 
     public float RotationSpeed = 200.0f;
 
 
     float speed = 0.0f;
     void Update()
     {
         //var x = Input.GetAxis("Horizontal") * Time.deltaTime * xFactor;
         //var z = 0.5f;
         //var y = Input.GetAxis("Vertical") * Time.deltaTime * yFactor;
 
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad0))
         //    z = Time.deltaTime * 0;
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad1))
         //    z = Time.deltaTime * 50;
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad2))
         //    z = Time.deltaTime * 100;
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad3))
         //    z = Time.deltaTime * 150;
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypad4))
         //    z = Time.deltaTime * 200;
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.R))
         //    z = Time.deltaTime * -50;
 
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad0))
         {
             speed = 0;
         }
         else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad1))
         {
             speed = 50;
         }
         else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad2))
         {
             speed = 100;
         }
         else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad3))
         {
             speed = 150;
         }
         else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypad4))
         {
             speed = 200;
         }
         else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.R))
         {
             speed = -50;
         }
 
         //transform.Rotate(y, 0, 0);
         //transform.Translate(0, 0, z);
         //transform.Rotate(0, x, 0);
 
         Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * xFactor, Input.GetAxis("Vertical") * yFactor, speed * zFactor);
         transform.Rotate(movement.y * Time.deltaTime, movement.x * Time.deltaTime, 0);
         transform.Translate(0, 0, movement.z * Time.deltaTime);
 
         Quaternion AddRot = Quaternion.identity;
         float roll = 0;
         float pitch = 0;
         float yaw = 0;
         roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
         pitch = Input.GetAxis("$$anonymous$$ch") * (Time.deltaTime * RotationSpeed);
         yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);
         AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
         GetComponent<Rigidbody>().rotation *= AddRot;
         Vector3 AddPos = Vector3.forward;
     }

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

245 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 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 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 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 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 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 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 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 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 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 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

Unusual Unit Slowdown 0 Answers

Controlling Character Speed via a Public Variable? 0 Answers

How to edit the Start Speed of a ParticleSystem where speed is within range using code 0 Answers

Is Something Wrong With My Singleton Pattern? 1 Answer

Can anyone explain what an offset is? 0 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