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 L500 · Aug 16, 2014 at 05:23 PM · collidertriggeraicolliders

Use different colliders

Hey, I'm new at using Unity and I'm creating a game with an ai following and attacking the player. I already wrote a script for following the player when it enters the collider but I want to use another collider where de player can be attacked in(closer to the player). How can I use different colliders in one script? This is my script right now:

 #pragma strict
 
 var target : Transform; 
 var moveSpeed : int = 2;
 var rotationSpeed : int = 3; 
 var goFollow : boolean = false; 
 
  
 function Follow(){
 transform.rotation = Quaternion.Slerp(transform.rotation,
     Quaternion.LookRotation(target.position - transform.position), rotationSpeed*Time.deltaTime);
  
  transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }
  
 function Start(){
   target = GameObject.FindWithTag("Player").transform; 
  }
  
 function Update (){
 if(goFollow == true){
 Follow();
 }
 }
  
 
 
 function OnTriggerEnter (other : Collider){
 if (other.tag == "Player"){
 goFollow = true;
 }
 }
 
 
 function OnTriggerExit (other : Collider){
 if (other.tag == "Player"){
 goFollow = false;
 }

Comment
Add comment · Show 1
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 PaulOrac · Aug 16, 2014 at 06:03 PM 0
Share

You can detect multiple triggers inside OnTriggerEnter, and, OnTriggerExit.

you could do:

 bool attack;
 
 function OnTriggerEnter (other : Collider){
 
 if (other.tag == "Player"){
 goFollow = true;
 }
 
 if(other.tag == "AttackingRange")
 attack = true;
 
 }
 
 
 function OnTriggerExit (other : Collider){
 if (other.tag == "Player"){
 goFollow = false;
 }
 
 if(other.tag == "AttackingRange")
 attack = false;
 
 }


Just make sure you add the corresponding collider with the tag "AttackingRange"

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryBurritoCoder · Aug 16, 2014 at 06:57 PM

I don't think you can in one script but you can easily make a similar script and make new game object with the new collider trigger on it and attach that new object to the player position and then add that attack script on the new object :)

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 Nick4 · Aug 16, 2014 at 06:51 PM

You can use :

 GetComponent(BoxCollider);
 

or :

 GetComponent(SphereCollider);

to get the specific colliders attached to your game object.

If you want to use same type of collider, you can create a child object and attach the same type of collider that you have attached to your parent object and access it this way :

 transform.GetChild(0).gameObject.GetComponent(BoxCollider); // 0 is the index of your child;
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 L500 · Aug 20, 2014 at 02:25 PM

But what am I doing wrong now?

pragma strict

var target : Transform; var moveSpeed : int = 2; var rotationSpeed : int = 3; var goFollow : boolean = false; var hitRange; var followRange; var attack : boolean = false;

function Follow(){ transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed*Time.deltaTime);

 transform.position += transform.forward * moveSpeed * Time.deltaTime;

}

function Start() { target = GameObject.FindWithTag("Player").transform; hitRange = GetComponent(SphereCollider); followRange = GetComponent(CapsuleCollider); }

function Update (){

if(goFollow == true) { Follow(); } }

function OnTriggerEnter (other : Collider) { if (other.tag == "followRange") { goFollow = true; }

if (other.tag == "hitRange") { attack = true; } }

function OnTriggerExit (other : Collider) { if (other.tag == "followRange") { goFollow = false; }

if (other.tag == "hitRange") { attack = 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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger-based vehicle ai? 1 Answer

Triggers? How do I use them to finish a game? 1 Answer

Temporarily ignore collisions between player and enemies 2 Answers

Problems with the Trigger Collider (randomly fictional) 1 Answer

Make object react to certain triggers only 1 Answer


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