- Home /
Getting radius of a sphere
Hi, I have the following Problem. I have a predefined Zone, (sphere). Once the Players enters that Zone via OnTriggerEnter, he will start to Auto fire. At the same time this Zone will shrink by reducing the scale of the object.
This all works great, if the Player leaves the Zone he stops Shooting etc. However if the Player moves into the Zone and does not move he will shoot even after the Zone has shrunk to a Point where the Player no more is colliding with the Zone.
As far as I can tell the reason is that when shrinking an object, even though the collision box also shrinks it does not check if it is still colliding with other objects.
So as solution I was thinking of getting the radius of the sphere and check in an update function if the Players position is within the radius else stop Shooting. I don't really know how to Access the radius of one object and get ist current size.
Here are my two scripts ;
Player Attack
var gun : Transform;
var bullet1 : GameObject; //Blue
var bullet2 : GameObject; //Yellow
var bullet3 : GameObject; //Red
var selectBullet : GameObject;
var mayShoot : boolean = false;
var fireRate : float;
function OnTriggerEnter (col : Collider)
{
if(col.gameObject.tag.Contains ("Zone")) //Check for tag containing zone
{
mayShoot = true;
InvokeRepeating ("Shoot", 0.0, 0.1 / fireRate);
shooting = true;
}
if (col.gameObject.tag == "Blue Zone") //choose bullet1 when entering blue zone
{
selectBullet = bullet1;
}
if (col.gameObject.tag == "Yellow Zone") //choose bullet3 when entering yellow zone
{
selectBullet = bullet2;
}
if (col.gameObject.tag == "Red Zone") //choose bullet3 when entering red zone
{
selectBullet = bullet3;
}
}
function OnTriggerExit (other : Collider)
{
if(other.gameObject.tag.Contains ("Zone"))
{
mayShoot = false;
}
}
function Shoot()
{
if (mayShoot == true && CombatZones.mayshoot2 == true)
{
Instantiate(selectBullet,gun.position, gun.rotation);
}
}
And CombatZones
var shrinkTimer : float;
static var mayshoot2 : boolean;
// check collision for player
function OnTriggerStay(other : Collider)
{
if (other.gameObject.tag == "Player")
{
StartCoroutine(LerpScale(shrinkTimer));
mayshoot2 = true;
}
}
//start shrinking
function LerpScale(time : float)
{
var originalScale = transform.localScale;
var finalX = transform.localScale.x;
var finalY = transform.localScale.y;
var targetScale = originalScale - Vector3(finalX, finalY, 0f);
var originalTime = time;
while (time > 0.0f)
{
time -= Time.deltaTime;
transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
yield;
}
}
function OnTriggerExit (other : Collider)
{
if (other.gameObject.tag == "Player"){
mayshoot2 = false;
}
}
function Update()
{
//Destroy when reaching 0
if (transform.localScale.x < 1)
{
mayshoot2 = false;
Destroy (gameObject);
}
}
Answer by teslatron · Jun 06, 2014 at 12:58 PM
If your object has sphere collider you can get the radius by:
float radius = GetComponent<SphereCollider>().radius;
Or you can get it from its renderer as well:
float radius = renderer.bounds.extents.magnitude;
Radius from sphere collider is relative to the size of the body, and doesn't change with size.
Answer by ravenclarico · Dec 31, 2021 at 11:05 PM
that's not quite correct. if your object doesnt have a scale of 1,1,1 then the actual radius of its sphere-collider is multiplied by the largest part of that object's scale.
so if the object's scale is: 3,2,1 ...
and the value from GetComponent().radius is: 2 ...
then the actual radius is: = 3 * 2 = 6.
you can get the actual radius in only 1 line of code by using this monstrosity:
float actualradius = GetComponent<SphereCollider>().radius*Mathf.Max(transform.lossyScale.x,transform.lossyScale.y,transform.lossyScale.z);
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
hide child object script - help 1 Answer
Instantiating at Collision Point. 2 Answers
Custom Collision Detection 4 Answers
Having problems with 2d collision triggers (javascript) 1 Answer