Help with using 3D code for a 2D game.(It's probably an easy fix)
I am trying to use someone's code that was written for a 3D game for a 2D game. My problem is that raycast is used for 3D games but raycast2D is used for 2D games(i think). Anyways, when i replace Raycast with Raycast with Raycast2D it has an error that starts at out.
I don't understand what out exactly does but when I try to cast out hit as a Raycast2D, it gives an error. Is there any way around this?
ViewCastInfo ViewCast(float globalAngle) { Vector3 dir = DirFromAngle(globalAngle, true); RaycastHit hit; if (Physics.Raycast(transform.position, dir, out hit, viewRadius, solid)) { return new ViewCastInfo(true, hit.point, hit.distance, globalAngle); } else { return new ViewCastInfo(false, transform.position + dir * viewRadius, viewRadius, globalAngle); } }
Raycast and Raycast2D don't use the same arguments. Raycast2D returns a struct with all the info ins$$anonymous$$d of a book.
Answer by theTrueJellyfish · Dec 24, 2019 at 07:00 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FOVscript : MonoBehaviour { public float viewRadius; [Range(0, 360)] public float viewAngle; public float meshResolution;
public LayerMask solid;
public MeshFilter viewMeshFilter;
Mesh viewMesh;
void Start()
{
viewMesh = new Mesh();
viewMesh.name = "View Mesh";
viewMeshFilter.mesh = viewMesh;
}
void LateUpdate()
{
DrawFieldOfView();
}
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.y;
}
return new Vector2(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
ViewCastInfo ViewCast(float globalAngle)
{
Vector3 dir = DirFromAngle(globalAngle, true);
RaycastHit hit;
if (Physics.Raycast(transform.position, dir, out hit, viewRadius, solid))
{
return new ViewCastInfo(true, hit.point, hit.distance, globalAngle);
}
else
{
return new ViewCastInfo(false, transform.position + dir * viewRadius, viewRadius, globalAngle);
}
}
void DrawFieldOfView()
{
int stepCount = Mathf.RoundToInt(viewAngle * meshResolution);
float stepAngleSize = viewAngle / stepCount;
List<Vector3> viewPoints = new List<Vector3>();
for (int i = 0; i <= stepCount; i++)
{
float angle = transform.eulerAngles.y - viewAngle / 2 + stepAngleSize * i;
Debug.DrawLine(transform.position, transform.position + DirFromAngle(angle, true) * viewRadius, Color.red);
ViewCastInfo newViewCast = ViewCast(angle);
viewPoints.Add(newViewCast.point);
}
int vertexCount = viewPoints.Count + 1;
Vector3[] vertices = new Vector3[vertexCount];
int[] triangles = new int[(vertexCount - 2) * 3];
vertices[0] = Vector3.zero;
for(int i = 0; i < vertexCount - 1; i++)
{
vertices[i + 1] = transform.InverseTransformPoint(viewPoints[i]);
if(i < vertexCount - 2)
{
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}
}
viewMesh.Clear();
viewMesh.vertices = vertices;
viewMesh.triangles = triangles;
viewMesh.RecalculateNormals();
}
public struct ViewCastInfo
{
public bool hit;
public Vector3 point;
public float dst;
public float angle;
public ViewCastInfo(bool _hit, Vector3 _point, float _dst, float _angle)
{
hit = _hit;
point = _point;
dst = _dst;
angle = _angle;
}
}
void OnDrawGizmosSelected()
{
Vector3 viewAngleA = DirFromAngle(-viewAngle / 2, false);
Vector3 viewAngleB = DirFromAngle(viewAngle / 2, false);
Gizmos.DrawWireSphere(transform.position, viewRadius);
Gizmos.DrawLine(transform.position, transform.position + viewAngleA * viewRadius);
Gizmos.DrawLine(transform.position, transform.position + viewAngleB * viewRadius);
}
}
This is my code but between lines 25 - 30 I need to make the raycast 2D so the script will work properly