Hi!
l have made a turret which shoots as player gets in range with mesh colider and attached this code to the range:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attackbox : MonoBehaviour
{
public TurretAI turretAI;
public bool isLeft = false;
void Awake()
{
turretAI = gameObject.GetComponentInParent<TurretAI>();
}
void OnTriggerStay2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
if (isLeft)
{
turretAI.Attack(false);
}
else
{
turretAI.Attack(true);
}
}
}
}
to me it seems it doesnt dedect when my player is in collider (maybe problem is somewhere else)
and actual turret script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretAI : MonoBehaviour
{
//Integers
public int curHealth;
public int maxHealth;
//Floats
public float distance;
public float wakeRange;
public float shootInterval;
public float bulletSpeed = 100;
public float bulletTimer;
//Booleans
public bool awake = false;
public bool lookingRight = true;
//References
public GameObject bullet;
public Transform target;
public Animator anim;
public Transform shootPointLeft;
public Transform shootPointRight;
void Awake()
{
anim = gameObject.GetComponent<Animator>();
}
void Start()
{
curHealth = maxHealth;
}
void Update()
{
anim.SetBool("Awake", awake);
anim.SetBool("LookingRight", lookingRight);
RangeCheck();
if(target.transform.position.x > transform.position.x)
{
lookingRight = true;
}
if (target.transform.position.x < transform.position.x)
{
lookingRight = false;
}
}
void RangeCheck()
{
distance = Vector3.Distance(transform.position, target.transform.position);
if(distance < wakeRange)
{
awake = true;
}
if(distance > wakeRange)
{
awake = false;
}
}
public void Attack(bool attackingRight)
{
bulletTimer += Time.deltaTime;
if (bulletTimer >= shootInterval)
{
Vector2 direction = target.transform.position - transform.position;
direction.Normalize();
if (!attackingRight)
{
GameObject bulletClone;
bulletClone = Instantiate(bullet, shootPointLeft.transform.position, shootPointLeft.transform.rotation) as GameObject;
bulletClone.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
bulletTimer = 0;
}
if (attackingRight)
{
GameObject bulletClone;
bulletClone = Instantiate(bullet, shootPointRight.transform.position, shootPointRight.transform.rotation) as GameObject;
bulletClone.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
bulletTimer = 0;
}
}
}
}