- Home /
gameobject destroyed on collision with character
hey guys , so i have a problem with my script , i have a character and a box ( i want the box to be destroyed when the character jump on the box) , the problem is , my script works only if the box hits the character , but me i want the box to be destroyed when the character hits the box not the opposite , here's my script ! thanks
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag == "Player"){
Destroy(gameObject);
}
}
I wrote an answer but then I realized I misunderstood your question. You say both objects can move, but only when the player moves onto the box should the box be destroyed, and not if the box moves onto the player. Is that right?
thanks for the reply @Diegzumillo , let me get this straight , in the part when i said when the box hits the character , i didn't mean the box can move ,but i positioned the box right on the top of the character (the box has a rigidbody),so when i hit the play botton , the box fall on the character , and the box gets destroyed , but when i positioned the box somewhere else , and i tried to collide with it with my character , the box won't get destroyed . i hope you will understand my problem , waiting for your reply ,thanks !
That's weird. Does your character have a rigidbody component too? if he doesn't have one your box might be 'sleeping' and it needs another rigidbody to wake up (or configured to never sleep)
no it doesn't have one , 'cause im using a character controller :s
Answer by aldonaletto · Nov 26, 2013 at 12:21 AM
Since the player is a CharacterController, you should use OnControllerColliderHit instead - but there's a problem: this event is only sent to the character script. A simple and efficient solution is to tag the boxes with some specific tag ("box", for instance), and place the code in the character script:
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.transform.CompareTag("box")){
Destroy(hit.gameObject);
}
}
Omg it's working ... omg it's working ,thanks a lot @aldonaletto , and if i want a child of my character to destroy the gameobjects (tagged as "box") when the child collide with them , cause i tried this script and i didn't work , BUT it worked perfectly with main character (y) , thanks again @aldonatello
This script only works when the object has a CharacterController. If you want to destroy the boxes when a child of the player hits them, make this child a trigger, attach a kinematic Rigidbody and use OnTriggerEnter in the child's script:
function OnTriggerEnter(other: Collider){
if (other.CompareTag("box")){
Destroy(other.gameObject);
}
}
works like magic (y) , now in my game , when the game starts the child is invisible until i press "right control" then he goes visible for 3 seconds and then goes invisible again , what i want is to destroy the box only if the child is visible can we like attach the script to the trigger so that the box will get destroyed only if the trigger is visible in that 3 seconds , thanks again @aldonaletto (y)
okay i made work perfect ,if anyone wanted to achieve something semiliar to me , here's how i deactivated the trigger for 1 second !
var isVisible : boolean;
var visibleTimer : float;
function Start(){
//on initialise el 3jeb hna
isVisible = false;
visibleTimer = 0;
// make the object invisible
isVisible = true;
collider.isTrigger = true;
}
function Update ()
{
if(Input.Get$$anonymous$$eyDown("right ctrl") && isVisible == false ){
// make the object invisible
isVisible = true;
collider.isTrigger = true;
}
// get our object visible
if(isVisible == true){
visibleTimer += Time.deltaTime;
if (visibleTimer >= 1.0){ // get the trigger invisible of 1 second
visibleTimer = 0;
isVisible = false;
collider.isTrigger = false;
}
}
}
Answer by ARRCOMM1 · Nov 26, 2013 at 04:34 PM
This looks interestingly like something I am trying to accomplish, EXCEPT, I want to make an Object Visible after I, the Player, Collides with it. Don't want to kill it. My PROBLEM is that I can not see where to plug in the Transform.Position and Transform.Rotation.
My code is:
using UnityEngine;
using System.Collections;
public class Invisibility : MonoBehaviour {
public Rigidbody SteppingStone9;
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Player"){
Rigidbody Step = Instantiate(SteppingStone9, transform.position, transform.rotation) as Rigidbody;
}
}
}
IF we REPLACED the Instantiate with the isVisible type control, it would work. I would not mind if the code was JS, vice, C#. I am not too good on JS, yet.
I am thinking something like this in JS ...
var isVisible : boolean;
function Start(){ //Step starts out Invisible isVisible = false;
}
function Update () {
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag == "Player"){
isVisible = true;
collider.isTrigger = true;
}
}
}
Answer by nyonge · Nov 26, 2013 at 02:11 AM
By just saying Destroy(gameObject) you're referencing the gameObject that the script is attached to - so if it's the box, it'll destroy the box. You can either specify the target of the collision to be destroyed
Destroy(collision.gameObject);
Or you can put the script onto the player, and make a new tag called like KillBox or something, then do:
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag == "KillBox"){
Destroy(gameObject);
}
}
That should work. I suspect it'd be a lot easier to do it the first way though ;)
Your answer
Follow this Question
Related Questions
Player flies upward off the map when it collides with a vertical mesh collider 1 Answer
Sphere collision "sticks" to Box collision 0 Answers
CharacterController:disable character-to-character collision 2 Answers
character jump on collision with a box 0 Answers
How to get a Character to move with a moving platform ? 6 Answers