Question by
Gohst002 · Jun 10, 2017 at 07:48 PM ·
raycastraycasting
RayCast Rotation
Hi guys. I watched brackey new tutorial on raycast but I cant get the raycast to rotate with the camera.
public float fireRate;
public float damage;
public float range;
public Camera fpsCam;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
Shoot ();
}
Debug.DrawRay (transform.position, Vector3.forward, Color.green);
}
public void Shoot()
{
RaycastHit hit;
if (Physics.Raycast (transform.position,Vector3.forward,out hit, range))
{
Debug.Log (hit.transform.name);
}
}
I cant help myself because I teach programming to myself and the scriping API hasn't helped.
Comment
Answer by canis · Jul 27, 2017 at 09:52 AM
I assume the script you already put on the camera. you shouldn't use "Vector3.forward" on line 22, instead you should use transform.forward therefore the vector will alway point to current transform forward.
or may be you can try this asset Raycast plugin
Your answer
Follow this Question
Related Questions
Is there any way how to do raycasts like this? 0 Answers
Raycasting not detecting collider 0 Answers
I'm Trying to get a 3D Model to follow a Raycast 0 Answers
I can't get RayCast to report the collider I want. 1 Answer
Blink, Dash, Raycasting 0 Answers