I was making a third person shooter and I want an item shop to buy better guns and skins so how do we do that. Can anyone recommend any good tutorials to do that?
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));
Answers
Also on how to make enemy ai that shoot gun at you, any tutorials?
@Axion
You should try the brackeys one where he makes a shop in tower defense this is the link - https://www.youtube.com/watch?v=uv1zp7aOoOs&list=PLPV2KyIb3jR4u5jX8za5iU1cqnQPmbzG0&index=8
@Axion
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