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 Xeong-Hu · Feb 28, 2014 at 08:54 AM · javascriptcollisionif-statements

How can i get my game to detect a collision?

I need help getting my game to detect collisions. I had help from my previouse question but i'm still stuck here.

So i decided to make a simple easy and readable code from my experience.

Here is the code of Collision the effect. Please test it because I'm starting to think my Unity3d is kind of bugged. Idk why it wont work.

 var collisionObject : GameObject; //My Cube
 var target1 : Transform; //My Cube
 var clicked : boolean;
 var speed : float;
 
 
 function OnCollisionEnter( hit : Collision){
  
     Debug.Log("Is "+ hit.tag+" the same as "+collisionObject.name+" ?");
  
     if (hit.gameObject.tag == "Zone1"//A tag placed on My Cube){
         Debug.Log("Result: Yes!");
         Occupied = true;
         if (Occupied == true){
             Debug.Log("This Spot is Occupied");
         }
     }else{ Debug.Log("Result: No ...");}
 }
 
 function OnMouseDown(){
 clicked = true;
 }
 
 function Update(){
     if (clicked == true){
                     
     var step = speed * Time.deltaTime;
                 // Move our position a step closer to the target.
     transform.position = Vector3.MoveTowards(transform.position, target1.position, step);}    
                 
     
 }

I'm simply moving my Object Sphere to my Object Cube.

The problem is. The OnCollisionEnter function acts as if it doesn't even exists.

Please help. Thank you.

Comment
Add comment · Show 5
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 fafase · Feb 28, 2014 at 08:55 AM 0
Share

Do you have a rigidbody on at least one of the colliding object?

avatar image Xeong-Hu · Feb 28, 2014 at 09:30 AM 0
Share

Yeah. I feel as if i did everything right but for some reason it's just not working.

I have a rigid body on both of my objects

avatar image fafase · Feb 28, 2014 at 09:36 AM 0
Share

Colliders on both as well?

avatar image fafase · Feb 28, 2014 at 09:38 AM 0
Share

Also, when you move, are you sure there is a collision? I mean that the sphere is not one frame on one side of the cube and then jumping to the other side next frame.

avatar image Xeong-Hu · Feb 28, 2014 at 09:55 AM 0
Share

Yep I got Colliders on Both of them. But i am noticing something fishy... When i press the sphere so it can move towards my Cube. It just go all the way inside of it. Am i missing a plugin? Or do i need to do sumthing with my movetowards..? Can you test this and see if it works fine for you?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Feb 28, 2014 at 10:04 AM

Collision are only detected for physic based movement. Meaning that if you manipulate the transform, the movement isn't processed by the physic engine and thus ignored.

You can use a kinematic rigidbody, but the other collider will have to be a trigger.

Check out this sample I did to summerize colliders behaviour : http://berengermantoue.fr/unitygames/smallprojects/collision_detection.html

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 Xeong-Hu · Feb 28, 2014 at 11:20 AM 0
Share

Alright tha7nks i tried out you u7nity flash which was pretty cool i 7never k7new u7nity had that. I'm goi7ng to look for more thi7ngs like that 7now. $But I tried a 7new code a7nd put. O7nTriggerStay i7ns$$anonymous$$d.

$But i still ca7nt get a collisio7n to de4bug.

 var collisionObject : GameObject; 
 var target1 : Transform;
 var clicked : boolean;
 var speed : float;
 
 
 function OnTriggerStay( hit : Collision){
  
     Debug.Log("Is "+ hit.tag+" the same as "+collisionObject.name+" ?");
  
     if (hit.gameObject.tag == "Zone1"){
         Debug.Log("Result: Yes!");
         Occupied = true;
         if (Occupied == true){
             Debug.Log("This Spot is Occupied");
         }
     }else{ Debug.Log("Result: No ...");}
 }
 
 function On$$anonymous$$ouseDown(){
 clicked = true;
 }
 
 function Update(){
     if (clicked == true){
                     
     var step = speed * Time.deltaTime;
                 // $$anonymous$$ove our position a step closer to the target.
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target1.position, step);}    
                 
     
 }

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

21 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

Related Questions

Checking if the two colliding objects have the same RGBA settings 1 Answer

Trying to proceed to next level when multiple items have collided 1 Answer

My Javascript collision isn't being detected, please help. 0 Answers

Player take damage on collision with AI 1 Answer

Finding a script on an object 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