hey guys,
Ive got this piece of code here which, what im trying to do is have the turret which the script is on, follow the target (“Player1”) when it is in range of the raycast which is on the turret. At the moment the raycast works to the extant that the hit test is fine but the locking on the target and rotation isnt would really appreciate some help thanks
#code
var LookAtTarget : Transform;
var rotate;
var rotationDamp = 2.0;
var distanceToPlayer;
var range = 100.0;
var hit : RaycastHit;
var right;
var target : Transform;
function Update () {
right = transform.TransformDirection(Vector3.right);
distanceToPlayer = Vector3.Distance(LookAtTarget.position, transform.position);
if(distanceToPlayer <= range)
{
if(LookAtTarget)
{
rotate = Quaternion.LookRotation (LookAtTarget.position - transform.position);
}
}
if(Physics.Raycast(transform.position,right,hit,range))
{
if(hit.collider.gameObject.tag == “Player1”)
{
shoot();
}
}
else {
rotate = Quaternion.identity;
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * rotationDamp);
Debug.DrawRay(transform.position,right * range, Color.red);
}
}
function shoot() {
Debug.Log(“shoot at player” + Time.time);
}
function Awake() {
LookAtTarget = GameObject.FindWithTag(“Player1”).GetComponent(Transform);
}
#code