Hi everyone!
I would like that when a bullet is shot, an explosion to be created at the entry of the weapon. So I created a particule system, linked to a prefab and a SpecialEffects script. But now I would like to do something like “if bullet shot → explosion” “else → do nothing” but I don’t know how to translate that in a C# language… I’m totally novice in scripting…
These are my WeaponScript and my SpecialEffectsHelper (sorry for the french comments, but they have no big importance ^^) :
using UnityEngine;
public class WeaponScript : MonoBehaviour
{
public Transform shotPrefab;
public float shootingRate = 0.25f;
private float shootCooldown;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
public void Attack(bool isEnemy)
{
if (CanAttack)
{
shootCooldown = shootingRate;
var shotTransform = Instantiate(shotPrefab) as Transform;
shotTransform.position = transform.position;
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
if (shot != null)
{
shot.isEnemyShot = isEnemy;
}
MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
if (move != null)
{
move.direction = this.transform.right; // ici la droite sera le devant de notre objet
}
}
}
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}
using UnityEngine;
///
/// Effets de particules sans efforts
///
public class SpecialEffectsHelper : MonoBehaviour
{
///
/// Singleton
///
public static SpecialEffectsHelper Instance;
public ParticleSystem explosionEffect;
void Awake()
{
// On garde une référence du singleton
if (Instance != null)
{
Debug.LogError("Multiple instances of SpecialEffectsHelper!");
}
Instance = this;
}
/// <summary>
/// Création d'une explosion à l'endroit indiqué
/// </summary>
/// <param name="position"></param>
public void Explosion(Vector3 position)
{
instantiate(explosionEffect, position);
}
/// <summary>
/// Création d'un effet de particule depuis un prefab
/// </summary>
/// <param name="prefab"></param>
/// <returns></returns>
private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
ParticleSystem newParticleSystem = Instantiate(
prefab,
position,
Quaternion.identity
) as ParticleSystem;
// Destruction programmée
Destroy(
newParticleSystem.gameObject,
newParticleSystem.startLifetime
);
return newParticleSystem;
}
}
I would be nice if someone could help me