Question by
Mujuice · Jul 13, 2017 at 01:55 PM ·
rotationraycasting
Shoot Raycast through RaycastHit point
I am working on a shooting system for my third person shooter game.
I am trying to shoot using 2 raycasts. I shoot first from the camera, which is located above the player. Then I am shooting the second one from the empty gameobject on the tip of the rifle.
The problem is that second raycast doesn't rotate when the player rotates. I tried rotating direction of the vector by multipliing it by the camera rotation quaternion. I played around with point's coordinate systems. Nothing helps. When I tried to cast a line, everything works and it rotates just as I need. I still need raycast though...
Code:
//Camera Ray
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
bool doShoot = Physics.Raycast(cameraRay, out cameraHit, Mathf.Infinity, LayerMask.NameToLayer("IgnoreShootingRaycast"));
//Gun Ray
Ray gunRay = new Ray(transform.position, transform.InverseTransformPoint(cameraHit.point).normalized);
Physics.Raycast(gunRay, out gunHit, Mathf.Infinity);
(Blue line - cameraRay, red line - gunRay)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612130156im_/https://answers.unity.com/themes/thub/images/avi.jpg)