Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by deathripper · Jun 06, 2014 at 12:11 PM · collisionsphereradius

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);
    }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TrollAxeThrower · Jan 02, 2019 at 03:19 PM 2
Share

Radius from sphere collider is relative to the size of the body, and doesn't change with size.

avatar image
0

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


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges