• 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 MilkX5 · Mar 06, 2013 at 12:45 AM · collisiontriggerdestroycolliderstarget

Target touching enemy collision

I can't figure out how to make a target block (isTrigger collider) that is attached to my character detect if it is touching an enemy(rigidbody with an attached isTrigger collider), and if so destroy it with the press of a button. I've tried OnTriggerStay but it doesn't exactly work. Here's what I have so far

 function OnTriggerStay ( other : Collider )    //Rigidbody a Character Controller must be on the the target enemy for this to work 
 {
     if (other.gameObject.tag == "enemy" && Input.GetButton("B"))
     {
         print ("Touching enemy");
     }
     if(other.gameObject.tag == "item" && Input.GetButton("B"))
     {
         print("I picked up an item");
     }
 }

Please help!

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 MilkX5 · Mar 09, 2013 at 01:12 AM

Nevermind I fixed my own problem after 5 days For those who need reference, this is my final result

var player : Transform;

 enum TargetTouch 
 {
     none = 0,
     item = 1,
     enemy = 2
 }
 
 var targetState = TargetTouch.none;
 var changeArea : boolean = false;
 
 var canAttack : boolean = false;
 var canPickup : boolean = false;
 
 var otherObj : GameObject;
 var kill : boolean = false;
 
 function Update () 
 {
     var playDir = player.GetComponent("1P Attack");
     kill = false;
     
     if ( changeArea )
     {
         SetTarget();
     }
     
     if (canAttack)
     {
         if(Input.GetButtonDown("B"))
         {
             Destroy(otherObj);
         }
     }
     if (canPickup)
     {
         if(Input.GetButton("A"))
         {
             Destroy(otherObj);
         }
     }
     
     /*if (playDir.direction == 0) //no buttons
     {
         transform.position = player.transform.position;
     }*/
     if ( playDir.direction == 1) //north
     {
         transform.position.x = player.transform.position.x;
         transform.position.y = player.transform.position.y + 1.1;
     }
     if ( playDir.direction == 2) //east
     {
         transform.position.y = player.transform.position.y;
         transform.position.x = player.transform.position.x + 1.1;    
     }
     if ( playDir.direction == 3) //south
     {
         transform.position.x = player.transform.position.x;
         transform.position.y = player.transform.position.y - 1.1;
     }
     if ( playDir.direction == 4) //west
     {
         transform.position.y = player.transform.position.y;
         transform.position.x = player.transform.position.x - 1.1;
     }
 }
 function OnTriggerEnter ( other : Collider )    //Rigidbody or a Character Controller must be on the the target enemy for this to work 
 {
 
     if (other.tag == "enemy" )//&& Input.GetButton("B"))
     {
         otherObj = other.gameObject;
         changeArea = true;
         print ("Touching enemy");
         targetState = TargetTouch.enemy;
     }
     if(other.tag == "item" )//&& Input.GetButton("A"))
     {    
         otherObj = other.gameObject;
         changeArea = true;
         print("I picked up an item");
         targetState = TargetTouch.item;
     }
 }
 function OnTriggerExit ( other : Collider )
 {
     print ("nothing");
     otherObj = null;
     changeArea = true;
     targetState = TargetTouch.none;
     changeArea = false;
 }
 function SetTarget ()
 {
     switch ( targetState )
     {
         case TargetTouch.none :
             canAttack = false;
             canPickup = false;
             changeArea = false;
             break;
         case TargetTouch.item :
             canPickup = true;
             canAttack = true;
             changeArea = false;
             break;
         case TargetTouch.enemy :
             canPickup = false;
             canAttack = true;
             changeArea = false;
             break;
     }
 }
 function Kill ( GameObject )
 {
     kill = 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Temporarily ignore collisions between player and enemies 2 Answers

Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer

Why won't the collision work? 3 Answers

onCollision Destroy 1 Answer

kinematic trigger with static collider-no trigger message? 0 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