- Home /
Handles.DrawLine from transform.forward in 2D
Working on a 2D implementation for a radius checker but wanted to visualize it in 2D, having some issues though. Switching the editor code fow.transform.up to fow.transform.forward gets the DrawWireArc on the Axis for 2D but I'm not able to figure out how to do this for the DrawLine Handle. Sebastian Lague has a great tutorial for this in 3D but getting it to work in 2d is proving more challenging than I anticipated. Field of view visualisation Any ideas?
Editor Code
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof (SimpleFOV))]
public class SimpleFOVEditor : Editor
{
Vector3 temp = new Vector3(1,-1,0);
private void OnSceneGUI()
{
SimpleFOV fow = (SimpleFOV)target;
Handles.color = Color.white;
Handles.DrawWireArc(fow.transform.position, fow.transform.up, Vector3.left, 360, fow.viewRadius);
Vector3 viewAngleA = fow.DirFromAngle(-fow.viewAngle / 2);
Vector3 viewAngleB = fow.DirFromAngle(fow.viewAngle / 2);
Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleA * fow.viewRadius);
Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleB * fow.viewRadius);
}
}
Scene Code
using UnityEngine;
using System.Collections;
public class SimpleFOV : MonoBehaviour
{
public float viewRadius;
[Range(0,360)]
public float viewAngle;
public Vector3 DirFromAngle(float angleInDegrees)
{
Vector3 point = new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}
Answer by starrtennis · Aug 08, 2019 at 11:03 PM
Hi! Thanks for the neat question. Can you post what the results look like as of now (the unsuccessful ones)? I would have thought, as you likely did, that it would in fact be a simple transform transform (pun unintended) from 3D coordinates to 2D coordinates.
A picture, or a screenshot, would be very useful to get a handle of what all the vectors, transforms, and angles are located in your 2D game as of now, to figure out how to modify it.
I'm guessing that instead of a top-down view with the view angles to the left and right, we have a side-forward view with the view angles up and down.