Answer the question
In order to leave comments, you need to log in
Scripts don't work, what's the problem?
I wrote a code that makes the tower shoot at enemies, (I followed the guide if I did anything), I checked for 30 minutes what was wrong, I wrote everything as it should be, but it still didn’t work. And yes, I assigned all scripts to prefabs.
Here is the guide: https://www.youtube.com/watch?v=AzKgQPocbes&t=1636s
Here is the bullet code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletTower : MonoBehaviour
{
public float Speed;
public Transform target;
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position,target.position,Time.deltaTime*Speed);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tower : MonoBehaviour
{
public Transform shootElement;
public Transform LookAtObj;
public float dmg = 5;
public float shootSpeed;
public GameObject bullet;
public Transform target;
public float shootDelay;
bool IsShoot;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (target)
LookAtObj.transform.LookAt(target);
if (!IsShoot)
StartCoroutine(shoot());
}
IEnumerator shoot()
{
IsShoot = true;
yield return new WaitForSeconds(shootDelay);
GameObject b = GameObject.Instantiate(bullet, shootElement.position, Quaternion.identity) as GameObject;
b.GetComponent<bulletTower>().target = target;
IsShoot = false;
}
}
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TowerTrigger : MonoBehaviour
{
public Tower twr;
public bool lockE;
public GameObject curTarget;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("enemyCube") && !lockE)
{
twr.target = other.gameObject.transform;
curTarget = other.gameObject;
lockE = true;
}
}
void Update()
{
if (!curTarget)
lockE = false;
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("enemyCube") && other.gameObject == curTarget)
lockE = false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question