basically i have a gun, infront of the gun there is an empty game object, i have attatched this script to it. basically i want the raycast line to shoot in the z axis(Vector3.forward) from this empty gameobject and when it hits another object with tag as "enemy" then destory it(for now). i know roughly how this is done but for some reason it isnt working. there is no errors just raycast doesnt work. and yes i have called this function in the update so that isnt the problem. and i have a variable for "public RaycastHit hit;"
here is my code so far:
public void Shoot()
{
if (Input.GetKey("f"))
{
if (Physics.Raycast(transform.position, Vector3.forward, out hit))
{
if (hit.collider.gameObject.tag == "Enemy")
{
Debug.Log("It Hurts");
Debug.DrawLine(transform.position, hit.point);
/* GameObject enemy = GameObject.FindWithTag("Enemy");
EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
enemyHealth.maxHealth = -10;*/
}
}
}
//else
}
}
i have extra code in there but i just commented it out for now i really just want the raycast to shoot forward from the position of which the gameobject that it is attatched to. thanks plz reply is u know how this is done im sure im not missing out that much. :)