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 John_Freeman · Dec 16, 2015 at 06:14 AM · movementspeedfloat

How to make my float return to it's original value after being changed.

So I have a movement script here and I want it to make it so when I press the left control key it cuts the movement speed in half. The code I have here does do that but when I let go of the left control key it leaves the slowed float at 0.5. I'm really new to programming and I can't figure out a way to make the value easily return back to 1 when the key is not pressed. I have my code below just so you guy's can see everything that's going on.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour
 {
 
     public float movementSpeed = 1.5f;
     public float slowed = 1f;
 
     void Update () {
 
         if(Input.GetKey(KeyCode.UpArrow))
         {
             this.transform.Translate(new Vector2(0,1) * (movementSpeed * slowed) * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.DownArrow))
         {
             this.transform.Translate(new Vector2(0,-1) * (movementSpeed * slowed) * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             this.transform.Translate(new Vector2(-1,0) * (movementSpeed * slowed) * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.RightArrow))
         {
             this.transform.Translate(new Vector2(1,0) * (movementSpeed * slowed) * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.LeftControl))
         {
             slowed = 0.5f;
         }
 
     }
 }
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 Knnthra · Dec 16, 2015 at 07:25 AM

This can be done a lot easier, but I understand that you are learning. However an easy fix for you would be to use:

 if (Input.GetKeyDown(KeyCode.LeftControl))
          {
              slowed = 0.5f;
          }
     if (Input.GetKeyUp(KeyCode.LeftControl))
         {
             slowed = 1;
         }


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 John_Freeman · Dec 16, 2015 at 07:45 AM 0
Share

No, I want the speed to be slowed by half when I press control and changing it to left shift will not change anything. it's just when the code here is run if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl)) { slowed = 0.5f; } It changes the float called slowed to 0.5 but it does not change back to 1 when I let go of the key.

avatar image Knnthra John_Freeman · Dec 16, 2015 at 10:12 AM 0
Share

I just mistook shift from control when I wrote the answer. Check my update on the answer. I know changing control to shift doesn't change anything. Notice that I'm using getkeydown ins$$anonymous$$d of getkey

avatar image John_Freeman Knnthra · Dec 17, 2015 at 12:41 AM 0
Share

Oh sorry. I misread the code. Thanks alot this worked.

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

33 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

Related Questions

increase speed of a character costantly 1 Answer

I want to make a PONG AI that can be beatable. but if i make a AI then either it will be unbeatable or ERROR. please help!!! 1 Answer

Stop moving gameObject and push it back 0 Answers

How can I determine the speed of my touch movements? 0 Answers

How to get rid of double movement speed? 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