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 nickneubrand · Aug 26, 2014 at 05:11 AM · collisiontriggerdestroy

Collider to Delete Object

Hi,

I'm trying to delete an object when it collides with a box only when a key is down. In this case the "j" key. I tried working with OnTiggerStay/OnTriggerEnter, which worked, but the response time was very bad. It would only seem to work when the object came in contact with the boxes edge. So in this example I have an object "HighHat", when it collides with any other object and the j key is down I want it to disappear. If anyone could help me out that would be great. I'm not to savvy with Unity or C#. So if you were to ask "why did you do ?" I unfortunately don't really have a good response. I did it because it worked the best at the time.

Thanks,

using UnityEngine; using System.Collections;

 public class DestroyHH : MonoBehaviour 
 {
     public int scoreValue;
     private GameController gameController;
     
     
     void Start ()
     {
         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null)
         {
             Debug.Log ("Cannot find 'GameController' script");
         }
     }
 
     void OnCollisionEnter(Collider other)
     {
     if (other.tag == "HighHat") 
     {
             if(Input.GetKeyDown (KeyCode.J))
             {
                 Destroy (other.gameObject);
                 gameController.AddScore (scoreValue);
             }
         }
     }
 }
 

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

1 Reply

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

Answer by S_Darkwell · Aug 26, 2014 at 05:45 AM

"Input.GetKeyDown" means that the button must be pressed at the precise moment that the object enters collision.

I suspect you may actually wish to use "Input.GetKey", which will check if when the collision occurs, the key is already pressed.

If you don't want the player to be able to simply hold down the key indefinitely and have the objects be destroyed any time a collision occurs, you can use a bool and a timer. When the player presses down J, set the bool and start counting down. If the objects collide while the bool is set, destroy the object. Once the timer reaches a certain point, set the bool to false. The player must then release the key, the timer will reset, then it can be pressed again. You can use this to give the player a very brief grace period (say 0.5 seconds) to hit the key before the collisions for them to destroy the object in question.

Hope this helps!

Comment
Add comment · Show 2 · 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 nickneubrand · Aug 26, 2014 at 06:25 AM 0
Share

I tried that and I keep getting an error

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

I think my issue is because I'm trying to do this with collisions and I may not have my project set up correctly.

avatar image nickneubrand · Aug 26, 2014 at 06:28 AM 0
Share

Actually this worked when I changed it back to OnTriggerStay. Im sure theyre are reasons why I should use a collider ins$$anonymous$$d of a Trigger, but it ended up working.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyHH : $$anonymous$$onoBehaviour 
 {
     public int scoreValue;
     private GameController gameController;
     
     
     void Start ()
     {
         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null)
         {
             Debug.Log ("Cannot find 'GameController' script");
         }
     }
 
     void OnTriggerStay(Collider other)
     {
     if (other.tag == "HighHat") 
     {
             if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.J))
             {
                 Destroy (other.gameObject);
                 gameController.AddScore (scoreValue);
             }
         }
     }
 }


Thanks a lot! -Nick

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is there anyway to make an object impenetrable? 1 Answer

Why is this rigidbody not activating the trigger? 0 Answers

Can't call Destroy() on two objects 0 Answers

Something wrong with destroying script 0 Answers

Collision not working for 2d platformer 2 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