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 PeppeJ2 · May 13, 2014 at 11:07 AM · c#collisionrigidbody

Check if other object has fired a Collision Event

Hey, I'm wondering if there's a way to check if a referenced object has collided with something. I know you can do this with a fairly simple script on the referenced object, but I want to know if it's possible to do this without setting up said script on the referenced object itself.

The reason I need this is because I'm making a simple pickup script using Joints to move an object around. Now I need this object to detatch when I smash it hard enough against a surface. I can't use the breakForce inherited on the Joint as it'll break if I move it too fast. So my idea is to check in my Pickup Script if the object I'm holding is colliding.

Pseudo Example:

 public GameObject heldObject;
 
 if(heldObject.collider.CollisionEnter)
 {
 //DoStuff
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · May 13, 2014 at 11:12 AM

Yes. Search google for 'Unity Collider'.

In the Collision functions, the Collider Argument is a container for the item that was hit by the collider. You have various access to this object through a call to the Collision. There are widespread examples of this on t'internet and right here on UA.

https://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

Comment
Add comment · Show 3 · 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 PeppeJ2 · May 13, 2014 at 11:22 AM 0
Share

I already know of this and that's what I said in the beginning of my post, this would be my "2nd solution".

That's the exact opposite of what I want. I want to run a script on my Camera, and then check if the object I referenced is colliding with something (That's not the camera). So something like

 public GameObject heldObj;
 
 if(heldObj.collider.CollisionEnter)
 {
 //DoStuff
 }
avatar image meat5000 ♦ · May 13, 2014 at 07:43 PM 0
Share

I've had a look and in my opinion you can not directly access the Collision state of a collider from a script in another object due to the way CollisionInfo is passed to the OnCollision Event functions. There does not seem to be a .colliding variable.

You can read or implant the variable value using GetComponent.

This can be achieved by making a Collision variable and copying the info to it during the Collision Event. Then this information will be available on any Object you wish to view provided this routine is in their script.

 //Script on Colliding Object
 #pragma strict
 
 var otherCol:Collision;
 
 function OnCollisionStay(col:Collision)
 {
     otherCol = col;
 }


 //Script on Observing Object
 #pragma strict
 
 var ob1 : GameObject;
 var ob2 : GameObject;
 
 var otherObjectScript : OCSTest;
 
 var testCol : Collision;
 
 function Start ()
 {
     ob1 = GameObject.Find("skeleton$$anonymous$$ecanimed");
     ob2 = GameObject.Find("skeleton$$anonymous$$ecanimed1");
     
     otherObjectScript = ob1.GetComponent(OCSTest);
 }
 
 function Update ()
 {
     testCol = otherObjectScript.otherCol;
     Debug.Log(testCol.gameObject + " COLLISION!");
 }

You could use a raycast or screentoworld point or something to acquire the objects ins$$anonymous$$d of a GameObject.Find

There probably are more elegant and cheaper solutions, but I wanted to give an example of what you could do. Hit me up if you know another way!

avatar image meat5000 ♦ · May 09, 2016 at 01:35 PM 0
Share

http://answers.unity3d.com/questions/762798/detect-collision-from-a-script-that-not-attached-t.html#answer-763059

avatar image
0

Answer by theredace · May 13, 2014 at 08:37 PM

Couldn't you just toggle a bool variable on/off as the object enters/exits the collider and check that variable state?

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 meat5000 ♦ · May 15, 2014 at 06:12 PM 0
Share

Yes, much the same way as above, but with limited information.

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

22 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

Related Questions

Restrict two objects from getting too close. 1 Answer

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

C# Problems with collision 0 Answers

rigidbody collision and forces 0 Answers

Stop sword from distorting character animation 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