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

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 :slight_smile:

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;

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