• 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 rhose · Jun 06, 2013 at 02:29 PM · boxcollider

Check is two game objects are colliding

I have an object (computer) rotating base on mouse input and a key pressed but i don't want my rotating object (a computer) to rotate if it breaks into the table surface. Example : picture

The script :

 function Update(){
     if (myGuiTexture.enabled == true){
         if (Input.GetKey(UnityEngine.KeyCode.Keypad1) || Input.GetKey(UnityEngine.KeyCode.Alpha1)){
         //here i want to check if the computer is colliding with the table
            //if not {
             rotation = Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
             myGameObject.transform.Rotate(rotation);
            }
         }
         
     }
 }
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 konashadow · Jun 06, 2013 at 03:01 PM 0
Share

Do you have colliders attached to the table and the computer?

avatar image rhose · Jun 06, 2013 at 03:02 PM 0
Share

i just attached them. next ?

1 Reply

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

Answer by ABetterBard · Jun 06, 2013 at 03:58 PM

I think this should work. Make sure to attach it to your computer gameObject and attach a rigidbody to it (turn off gravity if you want to avoid weirdness). On your table, make a new tag called "world" and add the table.

Both objects should have colliders attached.

 var oldPosition: Transform;
 
 function Update()
 {
     oldPosition = transform.position;

     if (myGuiTexture.enabled == true)
     {
        if (Input.GetKey(UnityEngine.KeyCode.Keypad1) ||  Input.GetKey(UnityEngine.KeyCode.Alpha1))
        {
            rotation = Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
            myGameObject.transform.Rotate(rotation);
        }
     }
 }
  
 
 function OnTriggerEnter (collider : Collider)
 {
       if(collider.transform.tag == "world")
       {
            transform.position = oldPosition;
       }
 }
 
Comment
Add comment · Show 5 · 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 Julien-Lynge · Jun 06, 2013 at 04:00 PM 0
Share

That's a good start, but there's one major issue

Once you enter the trigger, you get stuck (can no longer move)

If you keep track of position every update (e.g. save 'oldPosition'), then when you encounter an OnTriggerEnter, ins$$anonymous$$d of setting canRotate to false, just set position to oldPosition. That way, whenever you intersect something, it'll bounce back out of it.

avatar image ABetterBard · Jun 06, 2013 at 04:01 PM 0
Share

Good looking out, ^^^^^ This is the answer you seek! :)

edit: updated my response to correctly reflect your feedback.

avatar image rhose · Jun 06, 2013 at 11:55 PM 0
Share

I attached the script and a rigidbody to the computer. The table has a tag and box collider. If i play the game the OnTriggerEnter function is called imediately and the object is flying on my scene. If I remove the rigic body, the function is never called and the rotation problem is not fixed.

avatar image konashadow · Jun 07, 2013 at 06:06 AM 0
Share

You need the rigidbody on the computer for the collision detection to work. However, I'm going to assume that the reason the OnTriggerEnter function is called immediately is because your computer is already resting on the table from the start.

avatar image rhose · Jun 07, 2013 at 06:29 AM 0
Share

The computer is in the air from the start (to not collide with the table). With the script on it and the rigid body, after i press 1 the object starts flying. Thanks for your help. I know that is an old position/transform problem. I will look into this later.

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

17 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

Related Questions

Box collider not lining up with box mesh? 1 Answer

Invisible barrier transform.Translate error 1 Answer

Object flips 90 degrees when I add a box Collider - strange 0 Answers

Getting bounds of submeshes 1 Answer

Problem in Collider 2 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