- Home /
No way to use Raycast infront of player according to rotation in 2D?
Im trying to raycast in front of the player. My game is in topdown perspective and the player can move freely (so simply doing Vector2.up
and Vector2.right
and so forth doesn't work most of the time).
So far I've painstakingly used switch cases for the most common angles, but this is very noticable during gameplay because most angles aren't covered. The problem is that there doesn't seem to be a transform.forward
equivalent for 2D, and because the the Physics2D.Raycast
method requires a Vector2
, and there's no way to convert transform.rotation.z
to that required Vector.
This the code I have so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootingBehaviour : MonoBehaviour
{
public GameObject Missile;
public float movementSpeed = 50;
public GameObject shootTowards;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit2D hit = CalcHit(transform.rotation.z);
if (hit.collider != null)
{
Destroy(hit.collider.gameObject);
Debug.DrawRay(transform.position, hit.collider.transform.position, Color.red);
Debug.Log(hit.collider.gameObject.name);
}
}
}
public RaycastHit2D CalcHit(float rotation)
{
Vector2 offsetPos = new Vector2(transform.position.x + 30, transform.position.y + 30);
switch (transform.rotation.z)
{
case 0:
return Physics2D.Raycast(offsetPos, Vector2.right, 1000f, 1 << 8);
case 45:
return Physics2D.Raycast(offsetPos, new Vector2(1, 1), 1000f, 1 << 8);
case 90:
return Physics2D.Raycast(offsetPos, Vector2.up, 1000f, 1 << 8);
case 135:
return Physics2D.Raycast(offsetPos, new Vector2(1.5f, 1.5f), 1000f, 1 << 8);
case 180:
return Physics2D.Raycast(offsetPos, Vector2.left, 1000f, 1 << 8);
case -135:
return Physics2D.Raycast(offsetPos, new Vector2(-1f, -1f), 1000f, 1 << 8);
case -90:
return Physics2D.Raycast(offsetPos, Vector2.down, 1000f, 1 << 8);
case -45:
return Physics2D.Raycast(offsetPos, new Vector2(0.5f, 0.5f), 1000f, 1 << 8);
}
return Physics2D.Raycast(offsetPos, Vector2.right, 1000f, 1 << 8);
}
}
Answer by SmokiestSpace6 · Apr 24, 2020 at 06:46 AM
Try Vector2.right, there's also up and then the negatives of both, but I usually use Vector2.right and as long as the player sprite is initially facing right this should work, otherwise you can use the others mentioned.,Try Vector2.right, theres also up and then the negatives of both, but I usually use Vector2.right and as long as the player sprite is initially facing right this should work, otherwise you can use the others mentioned.
The problem is is that I need more than those basic few angles. I'm trying to make it that I can cast rays in all directions depending on my rotation around the z axis, not just the few that Vector2 provides. I already used Vector2.right, .up etc.
Your answer
Follow this Question
Related Questions
Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer
Top Down Shooter Player Controller 1 Answer
2D raycast shooting in the wrong direction 1 Answer
RaycastHit2D to hit an enemy when player rotates. (Using keyboard input only) 2 Answers
Raycasting from object rotation? 1 Answer