Help with Editing Code
Hello,
I have a simple piece of code that will spawn bullets for my enemy when I press a button, but I would like to edit this code so that the bullet destroys itself after 7 seconds. I also would like the enemy to only shoot when it my player enters the range of the enemy and have to wait 2 seconds before shooting at the player again. Any help is welcomed and thanks in advance!!
Code that makes enemy follow player when player enters a certain range:
**using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
public float lookRadius = 50f;
Transform target;
UnityEngine.AI.NavMeshAgent agent;
// Use this for initialization
void Start () {
target = PlayerM.instance.player.transform;
agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
}
// Update is called once per frame
void Update () {
float distance = Vector3.Distance (target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination (target.position);
if(distance <= agent.stoppingDistance)
{
//attack
FaceTarget();
}
}
}
void FaceTarget ()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position, lookRadius);
}
}**
Code that shoots when a button is pressed and I would like to have edited to do the things listed above:
**using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyShooting : MonoBehaviour { public Rigidbody slimeBallPrefab; public Transform enemyMouth; void Update () { if(Input.GetButtonDown("Fire1")) { Rigidbody rocketInstance; rocketInstance = Instantiate (slimeBallPrefab, enemyMouth.position, enemyMouth.rotation) as Rigidbody; rocketInstance.AddForce (enemyMouth.forward 5000); } } }*
Thanks again!! :)