- Home /
2D Raycast not working
Currently I've been stuck for a few hours browsing the web and trying all sort of things to get my 2D raycasts working. But so far I have no succes.
I have a world generated with a mesh generator. It has a mesh filter, mesh renderer and EdgeCollider2D on every single edge (the game is top-down). Collisions work perfect, with players and enemies. But the 2D raycast isn't detecting the wall.
The player (which also has the RayCast script) has a z-position of 0 and so does the wall. The wall has a tag named "Wall" layer default and the player has the tag "Player" and layer default.
Here is the code:
using UnityEngine;
public class RayCast : MonoBehaviour
{
private Vector3 _velocity;
private Rigidbody2D _rigidbody;
private void Start()
{
this._rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
this._velocity = this._rigidbody.velocity;
Debug.DrawRay(this.transform.position, this._velocity.normalized * 3, Color.green);
RaycastHit2D hit = Physics2D.Raycast(transform.position, this._velocity.normalized * 3);
if (hit)
{
if (hit.collider.tag == "Wall")
Debug.Log("Collision detected!");
}
}
}
I've tried everything is can imagine so far, also the 3d variant, anyone any idea what I might be doing wrong and how I could fix this?
Your answer
Follow this Question
Related Questions
2D click on object with a raycast not working 2 Answers
Is there a better way to find where to start my raycast? 2 Answers
My Raycasts seem to sometimes miss 0 Answers
Is there a problem with my raycast? 1 Answer
Ray is not being detected 0 Answers