- Home /
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;
}
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"
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 :)
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;
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; } }