using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Turret : MonoBehaviour
{
public Transform target;
public float speed = 1.0f;
void OnTriggerStay(Collider col)
{
target = FindClosestEnemy();
Vector3 targetDirection = target.position - transform.position;
float singleStep = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
Debug.DrawRay(transform.position, newDirection, Color.red);
transform.rotation = Quaternion.LookRotation(newDirection);
}
public GameObject FindClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Human");
GameObject closest;
closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
}
Turret.cs(11,18) Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’
I do not understand what is wrong or how to fix this and google isnt turning up any results
theres no actual weapon for the turret yet. I am just trying to do this in smaller bite sized steps as I am not proficient in c#. This current script is trying to search for the closest player/NPC, and then it will rotate the weapon (a dish thats supposed to represent a parabolic mirror) to aim at its target