- Home /
Question by
CsapiTheOne · Oct 20, 2020 at 11:54 AM ·
scripting problemturret
I need a turet range script or some fix for this one,How to add a range to this turret code ?
This is the script of the turret :/ ( I need to create a new script for this or its posible to fix this one ?)
public class ETurret : MonoBehaviour { private GameObject target; private bool targetLocked;
public GameObject turretTopPart;
public GameObject bullet;
public GameObject bulletSpawnPoint;
public float fireTimer;
private bool shotReady;
private void Start()
{
shotReady = true;
}
// Update is called once per frame
void Update()
{
if (targetLocked)
{
turretTopPart.transform.LookAt(target.transform);
turretTopPart.transform.Rotate(0, 90, 0);
if (shotReady)
{
Shoot();
}
}
}
void Shoot()
{
Transform _bullet = Instantiate(bullet.transform, bulletSpawnPoint.transform.position, Quaternion.identity);
_bullet.transform.rotation = bulletSpawnPoint.transform.rotation;
shotReady = false;
StartCoroutine(FireRate());
}
IEnumerator FireRate()
{
yield return new WaitForSeconds(fireTimer);
shotReady = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
target = other.gameObject;
targetLocked = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Why the cannon is never rotate looking at the target ? 1 Answer
I need help converting this script from zilch to js 0 Answers
How can i rotate both turret cannon and body same time or separate ? 1 Answer
How to Align/Orient a Bullet/Projectile along/with a Gun/Turret? 2 Answers
Unity 3D: How to make the game object speed increase in tapping continually? 0 Answers