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
2
Question by redwolfplayer · Aug 19, 2014 at 09:18 AM · movementrigidbodycube

Getting cube movement down

Hello people!

So I'll start this off by saying I'm extremely new an Unity, and after spending a LOT of time googling and reading through these answers I have to say I'm still completely stumped with my problem.

So for the game I'm playing around with, I have a cube on a plane that I want to move. However, if I use rigidbody.velocity to move the cube around it rotates everywhere, and even if I freeze the Y position and all of the rotations it still moves wonky which is expected when things are based on velocity. (takes awhile to slow down etc)

So I tried transform.position, however if I use that then the collision detection provided to me with rigidbody is not placed properly, and things go crazy.

Basically, all I want is the cube to move on the X and Z axis with no rotation smoothly, and to collide properly with walls that I make.

Thanks for any help!

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

Answer by kevinspawner · Aug 19, 2014 at 10:27 AM

  1. Make sure you have collision on both game objects. IN your case plane and a cube. Box collider is cheap and faster compared to mesh collider. But for testing purposes you can use mesh collider it will have a proper collision

  2. Add Rigidbody to the cube which will have the physics property.

  3. In the script. You can use Rigidbody. Addforce to create a force and can move the object in x and z axis. Also for keyboard inputs you can use something like if((Input.GetKeyDown(KeyCode.W)))

  4. paste the code in the script and attach it to the cube.

      void Update ()
         {
             float MoveForward = Input.GetAxis("Vertical") * 10 * Time.deltaTime;
             float MoveRotate = Input.GetAxis("Horizontal") * 10 * Time.deltaTime;
     
             transform.Translate(Vector3.forward * MoveForward);
             transform.Rotate(Vector3.up * MoveRotate);
     
         }
    
    
    
    
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 redwolfplayer · Aug 19, 2014 at 04:39 PM 0
Share

Thank you very much!!

avatar image
0

Answer by bubzy · Aug 19, 2014 at 11:32 AM

you shouldn't really alter the velocity of a rigidbody, instead use addForce()

 public float speed = 1.0f;
         
     // Update is called once per frame
     void Update () {
     
         if(Input.GetKey(KeyCode.UpArrow))
         {
             rigidbody.AddForce((Vector3.up)*speed);
         }
     }


of course, you can just alter the Vector3.up to match the direction that you want your object to travel in

hope this helps :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trouble with moving cube when key is pressed 1 Answer

Cube rotates to the left on its own when moving 1 Answer

Erratics movements from my rigidbody : why does it behave like that? 0 Answers

Rigidbody slows down before touching its target 1 Answer

2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 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