- Home /
Enabling / Disabling Collider Help
Hello! I have 2 objects (magazine release button / magazine) parented to a gun I made with simplified mesh colliders on each. When either are selected the magazine is released from the gun. My problem is I would like only the button collider enabled for the release animation when pressed, and the magazine collider enabled only for the reload. Is this possible? Here is my code:
var anim: Animator;
var OutHash : int = Animator.StringToHash("Out");
var InHash : int = Animator.StringToHash("In");
var button : GameObject;
var magazine : GameObject;
function Start()
{
anim = GetComponent("Animator");
magazine.collider.enabled = false;
}
function Update()
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit) == true && hit.transform.tag == "MagazinePoint" )
{
if(Input.GetMouseButtonUp(0))
{
anim.SetTrigger(OutHash);
button.collider.enabled = false;
magazine.collider.enabled = true;
}
if(Input.GetMouseButtonUp(0))
{
anim.SetTrigger(InHash);
magazine.collider.enabled = false;
button.collider.enabled = true;
}
}
}
Any help would be more than appreciated!!
The above code only allows the button collider to work, not the magazine collider.
Answer by Painstake · Dec 01, 2014 at 04:00 PM
Okay so it took a while but basically I had to play around with collider positions. Instead of enabling/disabling I created shadow collider objects that move with the animation (their own separate animation). This gives the appearance of colliders enabling/disabling, without actually doing so.
var anim: Animator;
var OutHash : int = Animator.StringToHash("Out");
var InHash : int = Animator.StringToHash("In");
var collider1OutHash : int = Animator.StringToHash("Collider1Out");
var collider1InHash : int = Animator.StringToHash("Collider1In");
var collider2OutHash : int = Animator.StringToHash("Collider2Out");
var collider2InHash : int = Animator.StringToHash("Collider2In");
function Start(){
anim = GetComponent("Animator");
}
function Update(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit) == true && hit.transform.tag == "MagazinePoint" ){
if(Input.GetMouseButtonUp(0)){
anim.SetTrigger(OutHash);
anim.SetTrigger(collider1OutHash);
anim.SetTrigger(collider2InHash);
}
if(Input.GetMouseButtonUp(0)){
anim.SetTrigger(InHash);
anim.SetTrigger(collider1InHash);
anim.SetTrigger(collider2OutHash);
}
}
}
I'm sure there is a code guru who can enlighten on a better way, but this works for my purposes. Hope this helps someone else as well.