The question is answered, right answer was accepted
Problems using RaycastHit2D.collider, detection only on the rays origin
I'm starting top-down shooter with a laser gun of sorts and I simply want it to detect when the laser fired by such gun hits a collider. For that I've been attempting to use RaycastHit2D. The problem for me is, when I fire the laser it only detects a hit when the collider (an enemy) crosses paths with the player, the origin of the ray. This script is attached to an invisible object child to the player, that also handles animation. Here's the function:
private void HandleHitDetection()
{
Vector2 playerPos = GameObject.FindGameObjectWithTag("Player").transform.position;
if (shoot)
{
RaycastHit2D hit;
hit = Physics2D.Raycast(playerPos, Vector2.zero);
if (hit.collider != null)
{
Debug.Log(hit.collider.name);
}
}
}
The bool used is set and reset from another script. Thanks for the help.
Answer by spicy_noodles · Apr 28, 2017 at 10:38 PM
This question was kind of dumb, the way i found to give direction to the ray was to do Vector2 objectPos = Camera.main.WorldToScreenPoint(transform.position); and then subtracting that to my mouse coordenates and making a vector2 with the new values Im dumb
Follow this Question
Related Questions
I think my raycast detects destroyed gameobjects 0 Answers
Issue with RaycastHit2D and object collider 0 Answers
A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer
Physics 2D with tile collider corner problem 2 Answers
Had to turn off gravity scale, but now player goes through walls 0 Answers