Hey everyone, I’m trying to cast a ray from my player object forward to see if a shot hits something. The problem is that I’ve only found a sample where the ray is casted from the camera’s position. Here’s the code:
void Update () {
if(Input.GetKey(KeyCode.Escape)){
Screen.showCursor = true;
}
if(Input.GetButtonDown("Fire1")){
Screen.showCursor = false;
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
Debug.Log(hit.transform.name);
Debug.DrawLine(transform.position, hit.transform.position, Color.red);
}
}
}
But since I’m working on a top-down shooter, I would like to cast the ray from the players transform position (transform.x, transform.y, transform.z+1) into a forward direction. transform.z+1 is there because the ray should start at the gun’s height.
I hope some of you can help me with that.