- Home /
Question by
cpmct32 · Oct 12, 2017 at 09:49 PM ·
c#particlesparticlesystemnot workingplay
ParticleSystem.Play() not working
When I try to play the particle system called impactEffect it won't work. I even wrote "Debug.Log(impactEffect.isPlaying)" on the very next line but it only puts out 'false' to the console.
using System.Collections;
using UnityEngine;
public class TurretControl : MonoBehaviour {
[Header("Attributes")]
public float turnSpeed = 10f;
public float shootingAngle = 40f;
public float range = 10f;
[Header("Bullets (default)")]
public float fireRate = 1f;
private float fireCountdown = 0f;
public GameObject bulletPrefab;
[Header("Laser")]
public bool isLaser = false;
public LineRenderer lineRenderer;
public ParticleSystem impactEffect;
[Header("Unity Requirements")]
private Transform target;
private bool oriented;
public string enemyTag = "Enemy";
public Transform partToRotate;
public Transform firePoint;
void Start () {
InvokeRepeating("UpdateTarget", 0f, 0.5f);
}
void UpdateTarget(){
// Getting a list of all enemies
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
// Looping through the enemies trying to find the closest one. Might be changed later for more precise target selection
foreach (GameObject enemy in enemies){
float distance = Vector3.Distance(transform.position, enemy.transform.position);
if (distance < shortestDistance){
shortestDistance = distance;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range){
target = nearestEnemy.transform;
} else {
target = null;
}
}
void Update(){
if (target == null){
if (isLaser){
lineRenderer.enabled = false;
}
return;
}
// Calculating the direction in which to look
Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
Vector3 rotationDifference = lookRotation.eulerAngles - partToRotate.rotation.eulerAngles;
if (Mathf.Abs(rotationDifference.y) <= shootingAngle){
oriented = true;
} else {
oriented = false;
}
if(!oriented) return;
if (isLaser){
laser();
} else {
if (fireCountdown <= 0){
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
Debug.Log(impactEffect.isPlaying);
}
void laser(){
if (!lineRenderer.enabled)
{
lineRenderer.enabled = true;
if (!impactEffect.isPlaying) impactEffect.Play();
Debug.Log(impactEffect.isPlaying);
}
lineRenderer.SetPosition(0, firePoint.position);
lineRenderer.SetPosition(1, target.position);
Vector3 dir = firePoint.position - target.position;
impactEffect.transform.position = target.position + dir.normalized;
impactEffect.transform.rotation = Quaternion.LookRotation(dir);
}
}
Comment
Perhaps you should instantiate the effect prefab (impactEffect) before attempting to play it.
That feels like an answer rather than a comment.
Answer by LightWell · Oct 13, 2017 at 06:20 AM
make sure impactEffect isn't returning null itself, so just print(impactEffect) and see what that returns. A screens shot the impactEffect game object as well as this script in the inspector would be really helpful.