0
\$\begingroup\$

Circle

In the screenshot, the object that is in the center and the circle is drawn around is the small spaceship.

What I'm trying to do is when the player or any object that moves towards the circle if the object touch or better if the object enters the circle area then do something.

For example, the cube in the screenshot is inside the circle area then do something like ontriggerenter and if the object moves out do something like in ontriggerexit. The same idea.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteAlways]
[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(1, 50)] public int segments = 50;
    [Range(1, 500)] public float xRadius = 5;
    [Range(1, 500)] public float yRadius = 5;
    [Range(0.1f, 5)] public float width = 0.1f;
    [Range(0, 100)] public float height = 0;
    public bool controlBothXradiusYradius = false;
    public bool draw = true;

    [SerializeField] private LayerMask targetLayers;
    [SerializeField] private LineRenderer line;

    private void Start()
    {
        if (!line) line = GetComponent<LineRenderer>();

        if (draw)
            CreatePoints();
    }

    private void Update()
    {
        if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
        {
            Debug.Log("player detected");
        }
        else
        {
            Debug.Log("player NOT detected");
        }
    }

    public void CreatePoints()
    {
        line.enabled = true;
        line.widthMultiplier = width;
        line.useWorldSpace = false;
        line.widthMultiplier = width;
        line.positionCount = segments + 1;

        float x;
        float y;

        var angle = 20f;
        var points = new Vector3[segments + 1];

        for (int i = 0; i < segments + 1; i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

            points[i] = new Vector3(x, height, y);

            angle += (380f / segments);
        }

        line.SetPositions(points);
    }

#if UNITY_EDITOR
    private float prevXRadius, prevYRadius;
    private int prevSegments;
    private float prevWidth;
    private float prevHeight;

    private void OnValidate()
    {
        if (!line) line = GetComponent<LineRenderer>();
        if (!line) return;

        if (!draw)
        {
            line.enabled = false;
        }
        else
        {
            line.enabled = true;

            if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth || height != prevHeight)
            {
                CreatePoints();

                prevXRadius = xRadius;
                prevYRadius = yRadius;
                prevSegments = segments;
                prevWidth = width;
                prevHeight = height;
            }

            if (controlBothXradiusYradius)
            {
                yRadius = xRadius;

                CreatePoints();
            }
        }
    }
#endif
}

I tried inside the Update to do :

if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
        {
            Debug.Log("player detected");
        }
        else
        {
            Debug.Log("player NOT detected");
        }

but it's not working even if I change in the Inspector the targetLayers to the player layer.

The player is not the cube in the screenshot and the player have a collider but the drawn circle don't have a collider. not sure how to do it with the Physics.CheckSphere or some other way but the idea is when the player or any other object enter the drawn circle area do something. maybe some how to calculate the distance from the circle ? not sure how to do it.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

The LineRenderer is not a good candidate for defining an area, because it doesn't even know it's a circle or even a closed polygon.

What I would do is add a child-object to the object with a SphereCollider of appropriate size which has the "Is Trigger?" flag set. You can then implement OnTriggerEnter, OnTriggerStay and OnTriggerExit accordingly. Keep in mind that this requires a Rigidbody on any objects it is supposed to detect.

\$\endgroup\$
1
  • \$\begingroup\$ A LineRenderer is fine for the visual representation (although personally I'd render the circle with some sort of specialty shader, so that it follows uneven geometry) but I would use an invisible collider as you described for the actual detection. \$\endgroup\$
    – jhocking
    May 7, 2021 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .