- Home /
how to change sphere/capsule collider, it sizes and the center
I made an enemy that can transform into ball and robot. The enemy start as a ball with sphere collider and when I get closer it transform into robot. The problem is when it transform into robot, it's need to change the collider onto capsule collider because it's legs don't have a collision so they go through the floor. I sucsseed to switch between the colliders tipes but when I tried to change the size and centers - it don't do that and don't change the collider tipe anymore. How do I change the collider tipe, size and centers?
This is what I did so far:
function Attack ()
{
if (canAttack)
{
rigidbody.AddForce(transform.up * 40);
animation.Play("TransformRobot");
yield WaitForSeconds (0.3);
Destroy(collider);
var capsuleCollider = GetComponent ("CapsuleCollider") as CapsuleCollider;
capsuleCollider.height = 5;
capsuleCollider.radius = 2;
capsuleCollider.center.y = -0.5;
canAttack = false;
}
}
function FindWaypoints ()
{
if (!canAttack)
{
rigidbody.AddForce(transform.up * 40);
animation.Play("TransformBall");
yield WaitForSeconds (0.3);
Destroy(collider);
var sphereCollider = GetComponent ("SphereCollider") as SphereCollider;
sphereCollider.radius = 2;
canAttack = true;
rigidbody.AddForce(transform.forward * 100);
}
}
Does your GameObject actually have both a CapsuleCollider component, and a SphereColliderComponent? You can have both and enable/disable one on-demand, so you don't have to keep changing dimensions on them.
Now I have (after you asked). But how I can enable/diable them when I need?
Answer by cityseeker · May 19, 2012 at 01:34 AM
Now I have (after you asked). But how I can enable/diable them when I need?
ANSWER: What i did was i created two empty game objects underneath my Player GameObject. I named one CapsuleCollider and another BoxCollider. I attached a Capsule Collider to the one named Capsule Collider and I attached a Box Collider to the one named BoxCollider. I have a GameMaster Script attached to another empty GameObject where I have created two public GameObject capsuleCollider and public GameObject boxCollider. From the inspector i drag the appropriate BoxCollider GameObject to the GameMaster public GameObject boxCollider field. Then I can just disable them in the script with
capsuleCollider = GameObject.GetComponent(<CapsuleCollider>);
capsuleCollider.enabled = false;
Your answer
Follow this Question
Related Questions
OnCollisionEnter, collider is the gameobject 1 Answer
Changing the size of a text object to only be as long as the letters 0 Answers
Scale script don´t work 0 Answers
Move sprite depending on its collider position 1 Answer
How to Move Player in Particular Direction Without holding Down a Button? 0 Answers