- Home /
Detecting contact on colliders?
Hi guys, I have a question, currently I am creating some sort of a bridge made of rocks, the bridge is supposed to be a trap, every time you stand on one of the rocks it will start going down into the boiling lava, if you get out of them they are supposed to return to it´s original position, I am trying to use an array to store every rock of the bridge but Im facing problems detecting the rocks in contact with the player, here i what I´ve got so far (pretty weird):
var rockSelected : GameObject;
var trigger : Collider; //Rock Collider
var rocks : GameObject[];
var triggerTouched : Collider[];
function Start(){
rockSelected = rocks[0];
trigger = triggerTouched[0];
if (rocks.length == 0) return;
if(triggerTouched.length == 0) return;
}
//ON-TRIGGER-STAY //***************
function OnTriggerStay(trigger:Collider){
if(trigger.CompareTag ("TrickyRock")){
Debug.Log("trap touched!!");
rockSelected.transform.Translate (0, -1 * Time.deltaTime, 0); //take down the rock slowly
}
}
Thanks for your time
you take down the rockSelected, but that rockSelected variable is constant? seems like you would send each touched rock to a Fall function of sorts. Currently, from what i understand, you want the triggered rock to fall s$$anonymous$$dily and not the "constant" rockSelected GO(of which i have no idea what it is). You could just add a line to the current setup, if im not mistaken up to this point, as such
rockSelected=trigger.transform;
rockSelected.Translate(....yada);
$$anonymous$$y first question is: how do you want the rocks to be returned to its original position? Does it reset to its original position, or does it rewind backwards to its original position(like in Prince of Persia!). Also are you using character controller or rigidbody on the player? Here's how I think is a better way of doing it: Sandwich the rocks inside a box of very thin static colliders, so it appears the rocks are in place. Now as soon as you step on it, you can attach a rigidbody to all six sides of the box and they all start falling. $$anonymous$$ake sure to store the positions and rotations of all the rocks and box faces into an array in when you have to reset the whole bridge.
I'm sorry I didn't put any code because I'm not sure if this is what you want. So do answer my questions above and I'll try and put up some code.
First You should read the topic http://unity3d.com/support/documentation/Components/class-$$anonymous$$eshCollider.html the most interesting part is at the bottom. Check the table.
Rocks and player need to have a collider. If You want to use triggers You need to check isTrigger = true; In my opinion You should use Rigidbody, non static , because You want to move the player and tricky rocks.
I wouldnt use an array. I would create a prefab of the bridge. $$anonymous$$ain static bridge is parent. Then make the tricky rocks children with all onCollisionEnter scripts and animation, or better make when player enters tricky rock set gravity to true of the rock. It will fall down. If You want the bridge to reset, You can move the tricky rock after for example 10 seconds after hiting the tag = "ground" ; set the position and rotation as it was previously.
If you parent it, do you move the parent along with the rocks? if you do that, all the rocks get affected too. If you keep the parent static and make the rocks fall, you can't reset it efficiently as you have to destroy the whole parent hierarchy an Instantiate it again. A simple built-in array can do wonders and are very very fast.
Your answer
Follow this Question
Related Questions
Preventing circles in dungeon generation? 0 Answers
Detection if a GameObject is below you or next to you? 1 Answer
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
Can't click gameobject when over another trigger? 1 Answer
OnTriggerEnter2D not Working 0 Answers