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 /
avatar image
0
Question by hannacho700 · Dec 03, 2018 at 12:21 AM · keyboardgetkey

Sometimes "GetKey" does not work. why?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Force : MonoBehaviour {
     private Rigidbody rb;
     int i = 0;
     public int limit_max = 1000;
     public int limit_min = 50;
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
         enabled = false;
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 temp = rb.velocity;
         float mag = temp.sqrMagnitude;
        
         if (Input.GetKey("a") && mag<limit_max || mag<limit_min)
         {
             rb.velocity *= 1.1f;
             Debug.Log(i + "  Accel");
         }
        
         if (Input.GetKey("s") && mag>limit_min || mag>limit_max)
         {
             rb.velocity *= 0.9f;
             Debug.Log(i + "  slow");
         }
         
         Debug.Log(i + " : " + mag);
         i++;
     }
 }
 

Sometimes "GetKey" works, Sometimes is not. I don't know Why...

Comment
Add comment · Show 1
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 IvovdMarel · Dec 03, 2018 at 12:38 AM 1
Share

You set enabled = false; in the Start. When do you enable it again? Your Update will only run with the script enabled.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Dec 03, 2018 at 12:40 AM

The problem isn't the GetKey, it's the velocity of the object. Once the object gets to zero velocity, it wont ever move again.


This is because you have a zero velocity object, and you multiply it by some acceleration or declaration number. Anything multiplied by zero, is zero.


 rb.velocity *= 1.1f;  // This won't move the object when it has zero velocity!!  This is zero!



Once magnitude hits zero, it will act like it doesn't work anymore.


Comment
Add comment · Show 2 · 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 Bunny83 · Dec 03, 2018 at 03:11 AM 0
Share

You are right about the acceleration wouldn't work if the velocity is 0. However if the velocity once is greater than 0 it can never reach 0 due to the || mag<limit_$$anonymous$$ which will force accelerate even without pressing "a" so velocity always stays above roughly "7" (==sqrt(50)). Likewise if the velocity goes over about 31.6 it will force a slow down. We don't ´know if the object has some initial velocity or not and if it's possible for the velocity to completely stop (due to collision). There are many things not clear. The enabled = false thing Ivovd$$anonymous$$arel mentioned in the comment may also play a role. The script might be enabled from the outside, though we just don't know.

avatar image hannacho700 · Dec 03, 2018 at 04:53 AM 0
Share

I also can't see Log on my console window.

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

100 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

Related Questions

Refactor code from getting input from keyboard to get input from UI Buttons 1 Answer

SOLVED - After using Unity Remote and accidentally hitting some button in Visual Studio, Input no longer detects keyboard unless the mouse is held down. Please, help. 0 Answers

How to get Key with KeyCode.Question? 1 Answer

Input.GetKey () does not work on mobile Bluetooth keyboard 2 Answers

Change graphics using keyboard 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