Question: So I am a beginner when it comes to Unity, and I'm trying to learn something new every week. This week, I am trying to

So I am a beginner when it comes to Unity, and I'm trying to learn something new every week. This week, I am trying to learn how to make radial triggers with gizmos and all using vectors. Here are my goals for what I'm trying to accomplish:

Detect if a game object is inside or outside my trigger

Draw the gizmo green if inside, and red if outside

Trigger should start from position of the game object it's attached to

Make sure the code fully functions correctly

Basically, my problem is trying to figure out how to do the calculations for the distance and to determine whether or not a gameObject is in the radius of another gameObject.

If anyone could help me figure this out, I would very much appreciate it.

This is what I have so far for my code:

using System.Collections;

using System.Collections.Generic;

using UnityEditor;

using UnityEngine;

public class RadialTrigger : MonoBehavior

{

[SerializeField]

private float radius = 4f;

[SerializeField]

private float distance;

[SerializeField]

private Transform objectB;

private void OnDrawGizmos()

{

//Calculate whether your in the radius or not

if(distance <= radius)

{

Handles.color = Color.green;

}

else

{

Handles.color = Color.red;

}

Handles.DrawWireDisc(transform.position, new Vector3(0,0,1), radius);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!