Here is my code:
using UnityEngine;
public class Raycast2DDetection : MonoBehaviour
{
[SerializeField] private float radius = 5.0f;
[SerializeField] private LayerMask detectionLayer;
[SerializeField] private Color sphereColor = Color.green;
[SerializeField] private float lineThickness = 2.0f;
private float sphereRadius = 3f;
private void OnDrawGizmosSelected()
{
Gizmos.color = sphereColor;
Gizmos.DrawWireSphere(transform.position, radius);
}
private void Update()
{
RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, Vector2.up, Mathf.Infinity, detectionLayer);
if (hit.collider != null)
{
Debug.DrawLine(transform.position, hit.point, Color.red, lineThickness);
Debug.Log("detected " + hit.point);
}
}
}
“isn’t working” isn’t useful.
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
Otherwise it is Time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log() to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
1 Like
Generally, you don’t want to assign values to variables in code when using the public modifier or [SerializeField], as Unity may overwrite the values.
The Debug.DrawLine takes a duration as the 4th parameter, but you’re giving it a line thickness.
1 Like