I am working on creating the shooting function of a sentry gun, but when I play the game the ray does not work properly, it just points in the same direction no matter where the sentry is pointing.
Here is the code:
function LookAt ()
{
// changes the rotation of the sentry based on the rotation of the sentry gun based on where the player (target) is
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
}
function Attack ()
{
var hit : RaycastHit;
var ray = transform.TransformDirection(transform.forward);
if (Physics.Raycast (transform.position, ray, hit, 100))
{
// if the ray hits the player, then it will tell me for now, later I will make it damage the player
if (hit.collider.gameObject.tag == "Player")
{
Debug.Log("The player was hit");
}
}
}
I tried to make comments so that you could see what things were supposed to do. Can anybody see an error in my script? There are no compiler errors, but the script doesn’t work like I want it to.
Thanks for your help