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 Vetm · May 18, 2014 at 04:37 PM · gameobjectcolliderboxcolliderbox

Tipping a cube over

My problem seems to be pretty easy but it looks like it is impossible to slove because you can't set a pivot of a 3D object in unity.

I have a tower in my game that needs to tip over and then stand up again. This has to work even if the object is resized and multiple times in a row. Is there any simple way to do this?

Here is a picture of how it should look like:

alt text

3.png (35.6 kB)
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
2
Best Answer

Answer by robertbu · May 19, 2014 at 08:00 AM

Here is a bit of sample code. Place it on a block and use the arrow keys:

 #pragma strict
 
 private var parent : Transform;
 private var dirs = [Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back];
 var speed = 90.0; 
 
 function Start() {
     parent = new GameObject().transform;
 }
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.LeftArrow)) {
         FlopTheBlock(Vector3.left);
     } 
     else if (Input.GetKeyDown(KeyCode.RightArrow)) {
         FlopTheBlock(Vector3.right);
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow)) {
         FlopTheBlock(Vector3.forward);    
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow)) {
         FlopTheBlock(Vector3.back);
     }
 }
 
 function FlopTheBlock(direction : Vector3) {
     var down = WhatsDown();
     var dir = transform.InverseTransformDirection(direction);
     
     var pos = dir * 0.5 + down * 0.5;
     pos = transform.TransformPoint(pos);
     
     var axis = Vector3.Cross(Vector3.down, direction);
     
     transform.parent = null;
     parent.rotation = Quaternion.identity;
     parent.position = pos;
     transform.parent = parent;
     
     var qTo = Quaternion.AngleAxis(-90.0, axis);
     
     while (parent.rotation != qTo) {
         parent.rotation = Quaternion.RotateTowards(parent.rotation, qTo, speed * Time.deltaTime);
         yield;
     }
 }
 
 function WhatsDown() : Vector3 {
     for (var i = 0; i < dirs.Length; i++) {
         if (Vector3.Dot(transform.TransformDirection(dirs[i]), Vector3.down) > 0.9)
             return dirs[i];
     }
 }

Note to solve this problem, there are a couple of sub-problems that need to be solved. First the rotation around an axis not through the pivot of the object. This code places an empty game object on an edge and make the visible object a child. So rotating the parent rotates the child around an edge.

To find the right edge, the code first finds which side of the cube is facing down. Then it translates the direction the cube is to be pushed into local coordinates. Combining the two gives a local coordinate of the edge point. Converting to world coordinates handles the scaling.

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 Vetm · May 19, 2014 at 05:17 PM 0
Share

Thank you so much! This helped me a lot! I almost gave up. It works great.

avatar image Loius · May 19, 2014 at 06:18 PM 0
Share

Don't forget to mark the answer as correct by clicking the check mark

avatar image
1

Answer by Sprakle · May 18, 2014 at 04:49 PM

Two things come to mind:

  • If you want to use the physics engine, apply a force at the top of the object.

  • If you want consistent deterministic movement, rotate around the base.

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

23 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

Related Questions

How do I make something happen at collider 1 Answer

Internal collisions 1 Answer

Access other game object BoxCollider 1 Answer

Multiple Colliders Trigger problem 2 Answers

Box collider on a default plane 0 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