- Home /
How can I control each npc from a main script on it's fire shooting rate of bullets force firepoints ?
I have one main script attach to empty gameobject name Shooting :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
[Header("Main")]
public List<GameObject> npcList;
public Rigidbody bulletPrefab;
public float launchForce = 700f;
public bool automaticFire = false;
public float bulletDestructionTime;
public LaunchProjectile launchprojectile;
[Space(5)]
[Header("Slow Down")]
public float maxDrag;
public float bulletSpeed;
private bool bulletsSlowDown = false;
public bool overAllSlowdown = false;
[Range(0, 1f)]
public float slowdownAll = 1f;
private List<Animator> anim = new List<Animator>();
private void Start()
{
for (int i = 0; i < npcList.Count; i++)
{
anim.Add(npcList[i].GetComponent<Animator>());
anim[i].SetBool("Shooting", true);
}
}
public void Update()
{
if (overAllSlowdown == true)
{
Time.timeScale = slowdownAll;
}
for(int i = 0; i < anim.Count; i++)
{
if (isAnimationStatePlaying(anim[i], 0, "AIMING") == true)
{
if (Input.GetButtonDown("Fire1") && automaticFire == false)
{
if (anim[i].GetBool("Shooting") == true)
{
anim[i].Play("SHOOTING");
launchprojectile.Launch(bulletPrefab,
launchForce, maxDrag, bulletSpeed,
bulletsSlowDown, automaticFire,
bulletDestructionTime);
}
}
else if (Input.GetButtonDown("Fire1") && automaticFire == true)
{
automaticFire = false;
}
else
{
if (Input.GetButtonDown("Fire2"))
{
automaticFire = true;
}
if (automaticFire == true)
{
anim[i].Play("SHOOTING");
launchprojectile.Launch(bulletPrefab,
launchForce, maxDrag, bulletSpeed,
bulletsSlowDown, automaticFire,
bulletDestructionTime);
}
}
}
}
}
bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
return true;
else
return false;
}
}
And this script is attached to each npc weapon fire points. Each npc have a weapon with two fire points:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaunchProjectile : MonoBehaviour
{
public void Launch(Rigidbody bulletPrefab,
float launchForce,
float maxDrag,
float bulletSpeed,
bool bulletsSlowDown, bool automaticFire,
float bulletDestructionTime)
{
Rigidbody projectileInstance = Instantiate(
bulletPrefab,
gameObject.transform.position,
gameObject.transform.rotation);
projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
if (bulletsSlowDown == true)
{
if (projectileInstance != null)
{
StartCoroutine(AddDrag(maxDrag, bulletSpeed, projectileInstance, bulletDestructionTime));
}
}
if ((automaticFire == true || automaticFire == false) && bulletsSlowDown == false)
{
projectileInstance.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
projectileInstance.gameObject.GetComponent<BulletDestruction>().Init();
}
}
IEnumerator AddDrag(float maxDrag, float bulletSpeed,
Rigidbody rb, float bulletDestructionTime)
{
if (rb != null)
{
float current_drag = 0;
while (current_drag < maxDrag)
{
current_drag += Time.deltaTime * bulletSpeed;
rb.drag = current_drag;
yield return null;
}
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.drag = 0;
rb.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
rb.gameObject.GetComponent<BulletDestruction>().Init();
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
And this is how the inspector looks like of the Shooting script :
And this is a screenshot of one the fire points of one of the npcs weapon with the script attached to it :
What I want to do is to see in the main script the Shooting script in the inspector each npc and under each npc to see it's properties like :
Fire shooting rate Fire shooting force Fire Points and to be able to shoot from one/two fire points Slowdown for each fire point bullets or for the whole two fire points
And maybe more properties but the idea is to organize the Shooting script to be able to control each npc fire points and bullets properties.
Answer by metalted · May 02, 2019 at 03:33 PM
You can use Serializable to show properties in the inspector. So i think you are looking for something like this:
using UnityEngine;
using System;
[Serializable]
public class Shooter
{
[Header("Properties")]
public bool canFire;
[Range(1,10)]
public int fireRate;
public GameObject firePoint;
}
using UnityEngine;
public class ShooterManager : MonoBehaviour
{
public Shooter[] shooters;
}