Hi! I’ve hit a bug while working on a 2D Unity project and I just can’t fix it
I hope the attached .gif file shows the problem, but to summarize; I’m making a laser reflection system that makes my laser bounce on some surfaces but not others. I tried this in 3D space before and it worked fine, but for some reason, it just won’t work in 2D space projects. As you can see, the line stutters and doesn’t reflect continuously like it should
I’ve added the code employed as well
Thanks
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(LineRenderer))]
public class PlayerController : MonoBehaviour
{
#region Movement
[Header("Movement")]
Rigidbody2D rigidbody;
[SerializeField] float moveSpeed = 10;
#endregion
[Space]
#region Laser data
[Header("Laser Data")]
LineRenderer lineRenderer;
Ray2D ray;
RaycastHit2D hit;
[SerializeField] LayerMask hitMask;
[SerializeField] Material laserMat;
[SerializeField] Transform emitter;
[SerializeField] Transform mesh;
[SerializeField] float totalLength = 30f;
[SerializeField] int numberOfReflections = 3;
#endregion
private void Awake()
{
InitRigidBody();
InitLineRenderer();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Move(GetInput(), moveSpeed);
UpdateRotation();
OnMainFireButton();
//CastRay();
}
void InitRigidBody()
{
rigidbody = GetComponent<Rigidbody2D>();
rigidbody.gravityScale = 0;
rigidbody.freezeRotation = true;
}
void InitLineRenderer()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.startWidth = lineRenderer.endWidth = .1f;
lineRenderer.material = laserMat;
}
Vector2 GetInput()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
input.Normalize();
return input;
}
void Move(Vector2 _input, float _speed)
{
rigidbody.velocity = _input * _speed;
}
void UpdateRotation() //Updates the player character's rotation based on the cursor's current position (Called in fixed update to fix a stuttering problem, idk why that happens, but this seems to fix it)
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90; //Subtract 90 degrees for adjustment
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
void OnMainFireButton()
{
if(Input.GetButton("Fire1"))
{
CastRay();
}
else
{
lineRenderer.positionCount = 0;
}
}
Vector2 DirectionReflection(Vector2 _direction, Vector2 _surfaceNormal)
{
Vector2 reflection;
_surfaceNormal.Normalize();
reflection = _direction - (2 * (Vector2.Dot(_direction, _surfaceNormal)) * _surfaceNormal);
return reflection;
}
void CastRay()
{
ray = new Ray2D(emitter.position, emitter.up);
lineRenderer.positionCount = 1;
lineRenderer.SetPosition(0, emitter.position);
float remainingLength = totalLength;
for(int i = 0; i < numberOfReflections; i++)
{
hit = Physics2D.Raycast(ray.origin, ray.direction, remainingLength, hitMask);
if (hit.collider != null)
{
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
remainingLength -= Vector2.Distance(ray.origin, hit.point);
ray = new Ray2D(hit.point, Vector2.Reflect(ray.direction, hit.normal));
if (hit.collider.tag != "Mirror")
{
break;
}
}
else
{
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
break;
}
}
}
}