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 adamchakov · Nov 25, 2013 at 11:21 PM · collisionphysicscharacterbox

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);
      }
 }
Comment
Add comment · Show 4
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 diegzumillo · Nov 25, 2013 at 11:44 PM 0
Share

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?

avatar image adamchakov · Nov 25, 2013 at 11:56 PM 0
Share

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 !

avatar image diegzumillo · Nov 26, 2013 at 12:06 AM 1
Share

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)

avatar image adamchakov · Nov 26, 2013 at 12:07 AM 0
Share

no it doesn't have one , 'cause im using a character controller :s

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
   }
 }
Comment
Add comment · Show 4 · 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 adamchakov · Nov 26, 2013 at 12:36 AM 0
Share

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

avatar image aldonaletto · Nov 26, 2013 at 12:40 PM 0
Share

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);
   }
 }

avatar image adamchakov · Nov 26, 2013 at 01:05 PM 0
Share

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)

avatar image adamchakov · Nov 26, 2013 at 01:32 PM 0
Share

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;
                 }
         }
  
 }
avatar image
-1

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;

                                           }
                                                   }
  }
Comment
Add comment · 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
0

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 ;)

Comment
Add comment · 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

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

20 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

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


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