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 SHiLLySiT · May 22, 2013 at 06:23 PM · rotationcollision

Checking for collision after rotation

I'm trying to rotate an object, only if the rotated position is collision free. The object is made up of several child objects, each of which have their own collider.

The way I've gone about doing this is rotating the object, then checking if the object is in a collision. If it is, the object is rotated back to its original position. However, it seems my collision check is always returning true, so the block is always rotated back. Any ideas? I have a feeling that the children are colliding with each other, which makes the collision check always return true.

I'd also like to note that the collision check works in another part of my code, where I'm checking if a new position is collision-free (without rotation) but doesn't seem to work here.

Rotation

 // block rotation
 if (Input.GetKeyDown(KeyCode.Q))
 {
     this.transform.Rotate(Vector3(0, 0, 90));
     
     // if rotating puts piece in collision
     if (Collide(this.transform.position.x, this.transform.position.y))
     {
         // rotate back
         this.transform.Rotate(Vector3(0, 0, -90));
     }
 }
 else if (Input.GetKeyDown(KeyCode.E))
 {
     this.transform.Rotate(Vector3(0, 0, -90));
     
     // if rotating puts piece in collision
     if (Collide(this.transform.position.x, this.transform.position.y))
     {
         // rotate back    
         this.transform.Rotate(Vector3(0, 0, 90));
     }
 }

Collision Check

 // moves peice to specified position and check children for collisions
 function Collide(x:Number, y:Number)
 {
     // save position
     var oldX:Number = this.transform.position.x;
     var oldY:Number = this.transform.position.y;
     
     // move to new position
     this.transform.position.x = x;
     this.transform.position.y = y;
     
     // make sure none of children collide
     var count:int = this.transform.GetChildCount();
        for (var i:int = 0; i < count; i++)
        {
            var block:Block = this.transform.GetChild(i).GetComponent("Block") as Block;
            if (block.Collide(block.transform.position.x, block.transform.position.y))
            {
                // move back to original pos
                this.transform.position.x = oldX;
                this.transform.position.y = oldY;
            
                return true;
            }
        }
        
        // move back to original pos
        this.transform.position.x = oldX;
        this.transform.position.y = oldY;
        
        return false;
 }

Block Collision Check

 // checks for collisions at the specified position
 function Collide(x:Number, y:Number)
 {
     return Physics.CheckSphere(new Vector3(x, y, this.transform.position.z), 0.48);
 }
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

1 Reply

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

Answer by Tomer-Barkan · May 22, 2013 at 06:53 PM

Yes, it's likely that the children are triggering collisions with each other, if there is overlap between their colliders.

There are two ways to solve this I can think of:

The first way is to use the physics layers so that when the children collide with each other, they don't trigger a collision event. It's very simple to do following this unity guide: http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

The second way would be to check that the parent is not the same in the collision handling event, and if it is, do nothing.

 void OnTriggerEnter(Collider other) {
     if (other.transform.parent == this.transform.parent) {
         return;
     }

     // revert the rotation here
 }
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 SHiLLySiT · May 22, 2013 at 07:45 PM 0
Share

Thanks, this solved my problem. Not sure why I didn't think about comparing the parents! :)

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

14 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

Related Questions

Collision problem 4 Answers

How to get the relative position of a Collider with OnControllerColliderHit()? 2 Answers

stop player when it hits a wall 2d 0 Answers

How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer

How do I stop my enemy from falling over? 3 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