It looks like you're new here. If you want to get involved, click one of these buttons!
i want to update my script to better then this
please any one download it and fix it to better please resend it here to share it for everyone to learn more and more building games 3D and 2D also 2.5D
Answers
srry i didnt say what i want
i want the emines follow player when he see him and attack him
also i want to add item drop
and i want a emies die this code not like this i dont know why
if i have some thing more i will tell here
@TheGhost
Hey
I fell that the script you are using are from the UCC asset which you need to buy and add to your project before being able to use them and what kind of attack do you want melee or shooting
@TheGhost
For the shooting enemy AI use this script
BTW the player Cube GameObject here is just a cube as child of player without mesh renderer & collider to reduce shot accuricy
if you want 100% shot accuricy just drag your player into that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public float LookDistance;
public float LOD;
public float FireRate = 15f;
public float ReloadTime = 2f;
[SerializeField]
private float NextTimeToFire = 2f;
[SerializeField]
private int maxEnemyAmmo = 50;
[SerializeField]
private int currentEnemyAmmo;
[SerializeField]
private int BulletsPerShot;
[SerializeField]
private bool isReloading;
public GameObject PlayerCube;
public Transform playerTrans;
public Transform GunTip;
public Animator enemyAnim;
public MyPlayerHealth playerHealth;
NavMeshAgent enemy;
public AudioSource EnemyShootSound;
public Cast cast;
void Start()
{
currentEnemyAmmo = maxEnemyAmmo;
enemy = this.GetComponent<NavMeshAgent>();
MyPlayerHealth playerHealth = gameObject.GetComponent<MyPlayerHealth>();
Cast cast = gameObject.GetComponent<Cast>();
}
void OnEnable()
{
isReloading = false;
enemyAnim.SetBool("Reloading", false);
}
public void Update()
{
if (isReloading)
return;
if (currentEnemyAmmo <= 0)
{
StartCoroutine(reloding());
return;
}
IEnumerator reloding()
{
isReloading = true;
enemyAnim.SetBool("Reloading", true);
yield return new WaitForSeconds(ReloadTime - 0.25f);
enemyAnim.SetBool("Reloading", false);
yield return new WaitForSeconds(0.25f);
currentEnemyAmmo = maxEnemyAmmo;
isReloading = false;
}
}
public void LookAtPlayer()
{
Vector3 direction = playerTrans.transform.position - this.transform.position;//Look at player
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(playerTrans.position, this.transform.position) < LookDistance)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
cast.Do();
}
}
}
Attach this script above on your my and add an empty gameObject where you want your enemy to shoot from (GunTip in this script) Ad the script bellow to your empty gameObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cast : MonoBehaviour
{
public MyPlayerHealth PH;
public float shootDist;
public EnemyAI EA;
void Start()
{
PH = gameObject.GetComponent<MyPlayerHealth>();
EA = gameObject.GetComponent<EnemyAI>();
}
public void Do()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
Debug.DrawRay(transform.position, Vector3.forward * shootDist);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(hit.collider.name == "Player")
{
PH.damage();
EA.LookAtPlayer();
}
}
}
}
Ad the script below to your player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayerHealth : MonoBehaviour
{
public int MaxHealth = 100;
public int CurrentHealth;
public int Damage = 1;
private void Start()
{
CurrentHealth = MaxHealth;
}
public void damage()
{
if(CurrentHealth >= 0)
{
CurrentHealth = CurrentHealth - Damage;
}
if (CurrentHealth == 0)
{
Debug.Log("Dead");
}
}
}
Hope that helps and dont forget to write the proper name of the scripts and scriptclass or you might get an error
Also you need to add a navMeshAgent component on your enemy and bake a NacMesh other wise the enemy wont follow
Ok i will check it now thanks for help