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
0
Question by Ikoh · Apr 09, 2015 at 10:04 AM · c#rotatemovecube

How to move/rotate cube forward/back/left/right in C#?

Hi everyone!

I'm trying to make a cube move and rotate (forward, back, right and left) using a C# script.

Still, I have a slight problem which is that I can't change the axis on which I move my cube any time I want. I can move/rotate it forward and back, no problem. I can move/rotate it right and left, no problem. But if I move it 2 times to the left and then forward for example, the cube doesn't move the way it should anymore.

Comment
Add comment · Show 2
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 Blackup · Apr 09, 2015 at 12:48 PM 0
Share

Are you hoping for a realistic physics-like topple? Or does the topple have to be an exact precise movement every time?

avatar image Ikoh · Apr 09, 2015 at 05:17 PM 0
Share

Hey Blackup and thank you for your comment. I'm looking for an exact precise movement every time since the cube is the player controller on a virtual board game (well, that's my goal anyway).

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by flashframe · Apr 09, 2015 at 02:01 PM

You just do this

http://answers.unity3d.com/questions/710087/tipping-a-cube-over.html#answer-710471


Note that if yOU DO want to do it with physics (rather than "abstractly" as in a puzzle game), then: Add a Rigidbody to your cube and add torque. (You'll need a floor under your cube)

     public float torque;
     public Rigidbody rb;
     void Start() {
         rb = GetComponent<Rigidbody>();
     }
     void FixedUpdate() {
         float turn = Input.GetAxis("Horizontal");
         rb.AddTorque(transform.forward * torque * turn);
     }

Tweak the torque variable until you get enough force to topple the cube.

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 Ikoh · Apr 09, 2015 at 05:20 PM 0
Share

Thank you flashframe. It's way easier indeed but I'm afraid I'm looking for a precise movement as if my cube were a player controller on a board game and you need to topple the cube exactly from tile to tile.

$$anonymous$$aybe there's a way to make your solution precise?

avatar image flashframe · Apr 09, 2015 at 05:38 PM 1
Share

Fair enough. How about using the script in this answer? It uses a parent object to rotate the cube, and finds the edge that is on the floor before rotating. Looks promising.

http://answers.unity3d.com/questions/710087/tipping-a-cube-over.html#answer-710471

avatar image Ikoh · Apr 09, 2015 at 06:42 PM 0
Share

Thank you so much! It works almost perfectly, I'm just gonna need to tweak it a little bit but that's great! Thanks again!

avatar image
2

Answer by Blackup · Apr 09, 2015 at 06:17 PM

Remember that with "transform.Up/forward/back/left/right/down" these are all relative to the object's orientation. The reason why your topples fail the second time... is because after the first topple, transform.up isn't up anymore. The object might now be lying on it's side, so transform.up might actually be pointing left...

So what does this mean? To make your rotations based on the world's notion of up, forward, right etc... you should think in WORLD space instead of local space... so use Vector3.up/down/right etc... instead. This will give you the world axis in those directions... they won't change and will always be the same no matter how many times your cube rotates.

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 Ikoh · Apr 09, 2015 at 06:45 PM 0
Share

I thought it was related to the local directions but I couldn't figure out how to solve the problem.

Flashframe gave me a JS script that works almost perfectly and which is less complicated than my C# script. I think I'm gonna use it but I'd like to correct my script anyway just for the heck of it and I believe your answer is gonna help me do that so thank you for your answer!

avatar image Blackup · Apr 10, 2015 at 07:01 AM 0
Share

Best exercise for understanding the logic is to translate the java script to c#. Sometimes this is hard work, but the experience and understanding is golden with all future DIY scripts. I'm very impressed with your script though. It shows a lot of intuition and initiative.

avatar image Fattie · Dec 12, 2015 at 07:22 PM 0
Share

It's true that the OP did not know about global coords, but the way this is done in vid games is just with a "rotational marker" parent. It's an everyday technique.

avatar image
0

Answer by Fattie · Dec 12, 2015 at 07:17 PM

This is a well-known trivial technique in vid game development. It's a duplicate of 100s of identical questions on here.

full tutorial ... http://answers.unity3d.com/questions/1111304/roll-cuboid-doesnt-work-properly.html#answer-1111309

You just move a "rotation point" around and then flexibly "re-parent" your cube to that.

It's a basic everyday technique to master in vid games.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Turning Spinners Based on an Integer 0 Answers

Why am I getting 2 different results with RotateAround? Trying to move cubes on surface of sphere. 0 Answers

Always rotates 0 when setting values 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