Question by
TubeGuy1 · May 07, 2018 at 04:31 AM ·
2draycastgameraycasting
Need a Raycast2d to point where my ship is looking
making a 2d go around game
I need a raycast2d to always point where the ship is pointing. The problem is that it always points to the right, left, up or down but not where my ship is pointing.
public float Velocity = 20;
public LayerMask wathtoHit;
public float maxRayDistance = 25;
void Update()
{
if(Input.GetKey("a")){
transform.RotateAround(Vector3.zero, Vector3.back, Velocity * Time.deltaTime);
}
if(Input.GetKey("d")){
transform.RotateAround(Vector3.zero, Vector3.forward, Velocity * Time.deltaTime);
}
if(Input.GetButton("Fire1")){
print("shot");
}
//this is where its called the function saying to position of the ray on the ship and where to point the ray
raycast(transform.position, Vector2.***!WheredoIPoint?!***);
}
void raycast(Vector2 origin, Vector2 direction){
//Ray2D ray = new Ray2D(transform.position, transform.position);
RaycastHit2D hit = Physics2D.Raycast(origin, direction, maxRayDistance, wathtoHit);
Debug.DrawLine(origin, direction + Vector2.up * maxRayDistance, Color.red);
if(hit.collider != null){
print("hit something" + hit.collider.name);
}
}
reference.png
(12.8 kB)
Comment
Your answer
Follow this Question
Related Questions
2D raycast not working 0 Answers
Select from multiple character to move 0 Answers
Raycast on mouse position problem. how not to raycast to 0 1 Answer