- Home /
Rotating weapon output Vector3[]
Hi, I have a ship that shoots out a lazer via Raycast and I set the position with Vector3[] so that I can add more or remove some locations later on, I also have a missile script thats similar but it creates an object that travels forwards and explodes. but the problem is that the output doesn't follow the ships rotation. heres my current script for the missile and lazers:
//missile script
public float maxReloadTime = 25f;
public string input = "Fire2";
public GameObject missile;
public Vector3[] weaponOutput;
Vector3[] globalPoints;
bool isReloading = false;
private void Start()
{
globalPoints = new Vector3[weaponOutput.Length];
for (int i = 0; i < weaponOutput.Length; i++)
{
globalPoints[i] = weaponOutput[i] + transform.position;
}
}
private void Update()
{
if (Input.GetButton(input) && isReloading == false)
{
StartCoroutine(ShootMissile());
}
}
IEnumerator ShootMissile()
{
if (weaponOutput != null)
{
for (int i = 0; i < weaponOutput.Length; i++)
{
Instantiate(missile, );
isReloading = true;
yield return new WaitForSeconds(maxReloadTime);
isReloading = false;
}
}
}
private void OnDrawGizmosSelected()
{
float size = 0.1f;
if (weaponOutput != null)
{
for (int i = 0; i < weaponOutput.Length; i++)
{
Gizmos.color = Color.red;
Vector3 globalWaypointPos = weaponOutput[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.forward * size, globalWaypointPos + Vector3.forward * size);
}
}
}
//Lazer script
public float damage = 10f;
public float force = 30f;
public float range = 100f;
public float fireRate = 15f;
public string input = "Fire1";
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public Vector3[] weaponOutput;
Vector3[] globalPoints;
float nTTF = 0f;
private void Start()
{
globalPoints = new Vector3[weaponOutput.Length];
for (int i = 0; i < weaponOutput.Length; i++)
{
globalPoints[i] = weaponOutput[i] + transform.position;
}
}
private void Update()
{
if (Input.GetButton(input) && Time.time >= nTTF)
{
nTTF = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
if (muzzleFlash != null)
{
muzzleFlash.Play();
}
if (weaponOutput != null)
{
for (int i = 0; i < weaponOutput.Length; i++)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, range))
{
HP hp = hit.transform.GetComponent<HP>();
if (hp != null)
{
hp.TakeDamage(damage);
Debug.Log(hit.transform.name + hp.Health);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * force);
}
if (impactEffect != null)
{
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
}
}
private void OnDrawGizmosSelected()
{
float size = 0.1f;
if (weaponOutput != null)
{
for (int i = 0; i < weaponOutput.Length; i++)
{
Gizmos.color = Color.red;
Vector3 globalWaypointPos = weaponOutput[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.forward * size, globalWaypointPos + Vector3.forward * size);
}
}
}
Comment