3

I use the code below to draw a circle around my game object:

using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(LineRenderer))]
 public class DrawCircle : MonoBehaviour
 {
     [Range(0, 50)]
     public int segments = 50;
     [Range(0, 5)]
     public float xradius = 5;
     [Range(0, 5)]
     public float yradius = 5;
     LineRenderer line;

     void Start()
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.positionCount = segments + 1;
         line.useWorldSpace = false;
         CreatePoints();
     }

     void Update()
     {
         CreatePoints();
     }

     void CreatePoints()
     {
         float x;
         float y;
         float z;

         float angle = 20f;

         for (int i = 0; i < (segments + 1); i++)
         {
             x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
             z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

             line.SetPosition(i, new Vector3(x, 0, z));

             angle += (360f / segments + 1);
         }
     }
 }

Let's say that in if I added this Circle class as a component of object A and object B. How would I be able to determine if the circle on Object A touches the Circle on object B?

Blaze
  • 71
  • 4
  • 10
  • If you are not using physics colliders you have to write your own "collision detection" functionality – UnholySheep Jan 27 '19 at 16:15
  • 1
    See e.g.: https://stackoverflow.com/questions/8367512/how-do-i-detect-intersections-between-a-circle-and-any-other-circle-in-the-same for the math required – UnholySheep Jan 27 '19 at 16:17

1 Answers1

3

If distance between centers of the circles is grerater than sum of its radiuses, the circles do not touch. if its smaller, they do

zambari
  • 4,797
  • 1
  • 12
  • 22