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 Xare · Mar 31, 2014 at 05:15 PM · collidersoncollisionenteroncollisionexitmousemove

OnCollisionEnter/Exit and Collider edges.

Hello everyone,

I have a project in which when you click on an object you select it. When selected, the object follows the mouse cursor. Now I want to have the selected object jump on top of any other objects that I point my mouse on. Here is my piece of code that controls the mouse movement and object collision detection.

 public bool isSelected;
 
 public List<Collider> currentlyCollidingWith = new List<Collider>();
     public GameObject lastCollidedObject;
 
     void OnCollisionEnter(Collision col){
         currentlyCollidingWith.Add(col.collider);
         lastCollidedObject=col.gameObject;
     }
 
     void OnCollisionExit(Collision col){
         currentlyCollidingWith.Remove(col.collider);
     }
 void OnMouseDown(){
         isSelected=!isSelected;
     }
 
 void FixedUpdate(){
         if(isSelected)
         {
             Vector3 distanceToCam = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,distanceToCam.z));
             if(currentlyCollidingWith.Count>0)
             {
                 transform.position=new Vector3(mousePos.x,lastCollidedObject.collider.bounds.max.y+gameObject.collider.bounds.extents.y,mousePos.z);
             }
             else
             {
                 transform.position=new Vector3(mousePos.x,gameObject.collider.bounds.extents.y,mousePos.z);
             }
             if(Input.GetKeyUp(KeyCode.Mouse1))
             {
                 transform.RotateAround(new Vector3(transform.position.x,transform.position.y,transform.position.z),new Vector3(0,1,0),45.0f);
 
             }
         }
     }


Also you can see my problem on this video as well:

http://www.youtube.com/watch?v=6ZoeNIA5kTs

The problem occurs only when an object's collider touches the edge of another collider. On such occasions, the collider returns a collision on one frame but no collision on the next frame which causes the object to change positions every frame.

Any help is appreciated. Thank you.

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 xandermacleod · Mar 31, 2014 at 05:31 PM 0
Share

It looks like your teleporting the cube up, and then immediately attempting to teleport it back down, because it's no longer within the bounding box of the other collider. I'd try swapping your line 23 if else statement around so that you check if it's equal to 0 first.

I've only given it a quick look though, so I could be wildly off.

avatar image Xare · Mar 31, 2014 at 05:43 PM 0
Share

Point is I teleport the cube up ONLY when it is colliding with something and I teleport it down ONLY when it is NOT colliding with something. Unity says it is colliding with the other cube one frame but says otherwise on the next frame and so on.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Runalotski · Jun 29, 2015 at 07:01 AM

Have you tried using OnCollisionStay() as that is called every frame that a collider is inside or touching another.

OnCollisionEnter() is only called on the frame where to colliders touch so in the next frame if there is still a collision the collider has not enterd or exited so CollisionEnter and Exit are not called.

Here is a link to the documentaion

http://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html

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

Collision returned from OnCollisionEnter2D and OnCollisionExit2D are not the same 1 Answer

Make object child on collision Make object child off collision 0 Answers

How to detect if a collider is touching a gameobject (java) 1 Answer

onCollisionExit2d problem, character doesn't get deparented 0 Answers

OnCollisionEnter2D and OnCollisionExit2D issue 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