- Home /
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.
Do you have a rigidbody on at least one of the colliding object?
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
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.
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?
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
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
Follow this Question
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