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 kingofthievesjutont · May 10, 2014 at 01:54 PM · rotationpositioncubeanglesflip

Flipping a cube while including the environment

I'm trying to emulate a cube-flipping motion similar to the gameplay of the game Edge. I found a code online which works great, but it unfortunately doesn't take the environment into consideration, due to the nature of the AlignBlock() function of the code.

My goal is to get the cube to make a check just before it "flops" for another cube in the way, or if it's on the edge of the floor. If there is another cube in the way, it should climb up on top of it, like in Edge, and climb down if it's on an edge.

I tried playing around with it, but I happen to be a beginner in unity coding, so I have no clue how to make the Quaternion.FromToRotation rotate correctly, which is where I suspect the problem is, though it could be somewhere else. Here is the code in question:

 var speed: float = 2.5; // flops per second
 var size: float = 1; // block size
 
 var flopping = false;
 
 function Update(){
 
     var move = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
     if (move.magnitude > 0.2) Flop(move);
 }
 
 function Flop(movDir: Vector2){
 
     var pivot: Vector3;
     var dir: Vector3;
     
     if (flopping) return; // ignore other commands while flopping
     flopping = true; // signals it's flopping
     if (movDir.y > 0){ // move forward?
         dir = Vector3.forward; // will flop forward
         pivot = Vector3(0,-1,1); // defines point around which rotate
     }
     else
     if (movDir.y < 0){
         dir = -Vector3.forward;
         pivot = Vector3(0,-1,-1);
     }
     else
     if (movDir.x < 0){
         dir = -Vector3.right;
         pivot = Vector3(-1,-1,0);
     }
     else
     if (movDir.x > 0){
         dir = Vector3.right;
         pivot = Vector3(1,-1,0);
     }
     AlignBlock(); // aligns block to grid before flopping
     // calculates the point around which the block will flop
     pivot = transform.position + (pivot * size / 2);
     var org = transform.position - pivot;
     var dest = (transform.position + dir * size) - pivot;
     var rot0 = transform.rotation;
     var rot1 = Quaternion.FromToRotation(org, dest) * rot0; 
     var a: float = 0;
     while (a < 1){
         var dt = Time.deltaTime * speed;
         a += dt;
         transform.position = Vector3.Slerp(org, dest, a) + pivot;
         transform.rotation = Quaternion.Lerp(rot0, rot1, a);
         yield;
     }
     if (audio) audio.Play(); // makes the flop sound 
     flopping = false;
 }
 
 private function AlignBlock(){
 
     var angles = transform.eulerAngles;
     // forces euler angles to be multiple of 90
     angles.x = 90 * Mathf.RoundToInt(angles.x / 90);
     angles.y = 90 * Mathf.RoundToInt(angles.y / 90);
     angles.z = 90 * Mathf.RoundToInt(angles.z / 90);
     transform.eulerAngles = angles;
     var pos = transform.position;
     // forces x and z to be in a grid 
     pos.x = size * Mathf.RoundToInt(pos.x / size);
     pos.z = size * Mathf.RoundToInt(pos.z / size);
     if (!rigidbody) pos.y = size * Mathf.RoundToInt(pos.y / size);
     transform.position = pos;
 }

Thanks in advance!

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Quick Angles Question 2 Answers

instantiating at different Angles? 1 Answer

Rotation along edge of a cube around another cube as its planet 1 Answer

Euler to Quaternion conversion returns unexpected result at exactly 180 degree rotation on Y axis 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