- Home /
Question by
iEnzo · Feb 01, 2019 at 08:06 AM ·
projectiles
What should I add to this script so the projectiles fired would change every 2 mouse clicks and return to its original state after it changes?
I'm trying to have the projectile change every 2 mouse clicks, and return to its original form after the changed projectile has been fired, what should I add to this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnProjectiles : MonoBehaviour {
public GameObject firepoint;
public List<GameObject> vfx = new List<GameObject>();
public RotateToMouse rotateToMouse;
private GameObject effectToSpawn;
void Start() {
effectToSpawn = vfx[0];
}
void Update() {
if (Input.GetMouseButton(0)){
SpawnVFX();
}
}
void SpawnVFX(){
GameObject vfx;
if (firepoint != null) {
vfx = Instantiate(effectToSpawn, firepoint.transform.position, Quaternion.identity);
if (rotateToMouse != null) {
vfx.transform.localRotation = rotateToMouse.GetRotation ();
}
} else {
Debug.Log ("No Fire Point");
}
}
}
Comment
Best Answer
Answer by Hellium · Feb 01, 2019 at 08:15 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnProjectiles : MonoBehaviour
{
public GameObject firepoint;
public List<GameObject> vfx = new List<GameObject>();
public RotateToMouse rotateToMouse;
// The bigger the value, the more clicks you will have to make in order to spawn the special effect
public int specialEffectThreshold = 2 ;
private GameObject effectToSpawn;
private int firesCount = 0;
void Update()
{
if (Input.GetMouseButton(0))
{
firesCount++;
// Replace vfx[1] by the special effect you want to spawn every 2 clicks
effectToSpawn = ( firesCount % specialEffectThreshold == 0 ) ? vfx[1] : vfx[0] ;
SpawnVFX();
}
}
void SpawnVFX()
{
GameObject vfx;
if (firepoint != null)
{
vfx = Instantiate(effectToSpawn, firepoint.transform.position, Quaternion.identity);
if (rotateToMouse != null)
{
vfx.transform.localRotation = rotateToMouse.GetRotation ();
}
}
else
{
Debug.Log ("No Fire Point");
}
}
}