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 Carson365 · Dec 20, 2017 at 08:37 PM · movementaddforcecubesmooth

How to move a cube constantly with GetKeyDown

Hello! I need to be able to press "A" and the cube will keep moving to the left until it hits a wall. I've tried to do this by using Rigidbody.AddForce, but what happens is it isn't constant then, and I have to unlock the rotation constraints, which need to be locked so it has a smooth movement.

I recommend trying your solution before you tell me because it might not work. Thanks and please tell me fixes!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DreadKyller · Dec 21, 2017 at 01:01 AM

While the answer Konomira gave is correct, I feel a few more explanations should be added, specifically why in the sample in their answer they use GetKey instead of GetKeyDown.

GetKeyDown() returns true only on the exact frame the key was pressed, and false until it's pressed again. GetKeyUp() does the opposite, inform you when the key was released. If you wish to get whether the key is held down, you use GetKey()

Forces are used for many things, but imagine a force like an object hitting another object, or wind, etc, it applies a sudden force that changes the angular velocity and the velocity of the object based on its pivot and mass, applying a force constantly will accelerate the object. In your case you don't care about rotation and since you're getting key presses it's probably a character script. In this case just setting the velocity through rigidbody.velocity is far more consistent.

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
avatar image
0

Answer by Konomira · Dec 20, 2017 at 10:04 PM

This should move the object left, relative to its rotation.

 void Update()
 {
   float speed = 1;  // Adjust this as necessary
   if(Input.GetKey(KeyCode.A)) // When 'A' is pressed
   {
     Vector3 v = -transform.right * speed;  // -transform.right = left
     GetComponent<Rigidbody>().velocity = v;  // Sets velocity to left movement
   }
 }
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 Konomira · Dec 20, 2017 at 10:10 PM 0
Share

This assumes you just want to tap the 'A' key. If you want it to be held down you could do else { GetComponent<RigidBody>().velocity = Vector3.zero;}

avatar image
0

Answer by Carson365 · Dec 21, 2017 at 01:19 AM

Thanks to you both, this information helped me out!

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

111 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

Related Questions

The Cube object moves through other cube objects and does not move smoothly on other cube objects,Cube object moves through other cube objects and does not move smoothly 0 Answers

Smooth Camera Movement script with problem. 0 Answers

Move along grid where facing 1 Answer

Moving an object using rigidbody.AddForce & keyboard input. 1 Answer

Change in position going up not smooth 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