• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by warlockoo1 · Jul 27, 2015 at 10:51 AM · unity 5javascriptrigidbodycollison

Rectangular Cube is sinking in floor rotating at 90 degree

Okay so I am trying to make a game like "bloxorz" in which a rectangular cube is rotated at 90 degrees. But when I try to implement it the cube is half sinking to the floor at 90 and then fully sink at another 90 degree. And I am using box collider and script that is given below. And I also attached an empty gameobject to parent cube. I am thinking that rigid body will help but I am new to unity so don't know how to implement it. And also that cube is working perfectly but only rectangular cube has this problem. Please help I am stuck at this.

Code for Rectangular Cube:

 private var ismoving : boolean = false;  
 private var startY : float = 0;  
 var cubeSpeed : float;  
 var cubeSize : float;
   
 function Update ()   
 {  
   
     if (Input.GetKeyDown("up") && ismoving == false)  
     {  
         ismoving = true;  
         transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);  
         StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));  
     }   
     if (Input.GetKeyDown("down") && ismoving == false)  
     {  
         ismoving = true;  
         transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);  
         StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));  
     }  
     if (Input.GetKeyDown("left") && ismoving == false)  
     {  
         ismoving = true;  
         transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);  
         StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));  
     }  
     if (Input.GetKeyDown("right") && ismoving == false)  
     {  
         ismoving = true;  
         transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);  
         StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));     
     }  
 }  
   
 function DoRoll (aPoint, aAxis, aAngle, aDuration) {    
   
  var tSteps = Mathf.Ceil(aDuration * 30.0);  
  var tAngle = aAngle / tSteps;  
  var pos : Vector3; // declare variable to fix the y position  
    
 // Rotate the cube by the point, axis and angle  
  for (var i = 1; i <= tSteps; i++)   
   {   
     transform.RotateAround (aPoint, aAxis, tAngle);  
     yield WaitForSeconds(0.0033333); 
     Debug.Log("Tsteps : " + tSteps); 
   } 
    
 // move the targetpoint to the center of the cube   
    transform.Find("targetpoint").position = transform.position;  
   
 // Make sure the y position is correct 
    pos = transform.position; 
    pos.y = startY;
    transform.position = pos;  
    
       
 // Make sure the angles are snaping to 90 degrees.       
    var vec = transform.eulerAngles;  
    vec.x = Mathf.Round(vec.x / 90) * 90;  
    vec.y = Mathf.Round(vec.y / 90) * 90;  
    vec.z = Mathf.Round(vec.z / 90) * 90;  
    transform.eulerAngles = vec;  
  
       
 // The cube is stoped  
    ismoving = false;       
 }  


Code for child GameObject:

  var Radius : float;
     
     function Update () {
         transform.rotation = Quaternion.identity;
     }
     
     function OnDrawGizmos(){
         Gizmos.DrawSphere (transform.position, Radius);
     }
 
 
 
Comment

People who like this

0 Show 6
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 Nido · Jul 28, 2015 at 09:37 AM 0
Share

If you put this in Update() or FixedUpdate()?

 // Make sure the y position is correct 
     pos = transform.position; 
     pos.y = startY;
     transform.position = pos;

Because inside DoRoll() you are calling this correction only once.

avatar image warlockoo1 · Jul 28, 2015 at 09:52 AM 0
Share

Thanks for the reply Nido. I tried that and put it in Update() but then also no success. And even when I rotate it in inspector then also it is half sunk in floor at 90 degree.

avatar image Nido · Jul 28, 2015 at 10:13 AM 0
Share

Hm... then without seeing how it's behaving I only may suggest to use a parent who will move arround and the cube controlling the rotation (use animation maybe?). Or moving the cube's pivot to the edge that will stay in the floor during the rotation. The second looks more accurated for your problem :)

avatar image warlockoo1 · Jul 28, 2015 at 10:39 AM 0
Share

Okay I am uploading pics so u guy can understand what I am saying.

alt text

alt text

okay see edges at cube is rotating. 1st step cube is at 90 degree and another is at 180 degree. Now after 1st step it should rotate using c edge but instead it rotate using b edge and turns 360 degree instead of 180 degree. And u can see that is half sink in floor in 1st pic and full sink in 2nd pic.

3.png (22.6 kB)
2.png (11.1 kB)
avatar image Nido · Jul 28, 2015 at 11:02 AM 0
Share

I see. I'll test an idea later.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody.Addforce not working in Unity 5.4.1 3 Answers

Ball getting stuck in surfaces and bouncing for no reason? Help! 0 Answers

hing joint issue,Hing Joint Issue in tow truck 0 Answers

How to loop unity?JS 3 Answers

Rigidbody -- Box Collider Collision Detection 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