Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unity3D GameObject(energy orb) movement similar to the Fable game series.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* Player must have tag set to "Player"
*
* Attach this script to a GameObject with a particle system.
*
* The particle will follow the player similar to the Fable energy orbs.
*
* TODO: You will need to setup actions in OnParticleCollision...
*/
public class ManaControl : MonoBehaviour {
private GameObject player;
private Transform targetPos;
public float speed = 1.0f;
public float distance = 5.0f;
private bool move = false;
private ParticleSystem m_ps;
private ParticleSystem.Particle[] m_Particles;
void Start(){
player = GameObject.FindGameObjectWithTag("Player");
m_ps = gameObject.GetComponent<ParticleSystem> ();
targetPos = player.transform;
StartCoroutine("DoCheck");
}
void Update () {
//Move in update for smooth transitions
if (move == true) {
var travelDir = (targetPos.position - gameObject.transform.position).normalized;
transform.position += travelDir * speed * Time.deltaTime;
}
//Check if all orbs are gone and destory
int numParticlesAlive = m_ps.GetParticles(m_Particles);
if (numParticlesAlive <= 0) {
Destroy (gameObject);
}
}
void ProximityCheck() {
if (Vector3.Distance (transform.position, targetPos.position) < distance) {
move = true;
} else {
move = false;
}
}
IEnumerator DoCheck() {
for(;;) {
ProximityCheck();
yield return new WaitForSeconds(1f);
}
}
void OnParticleCollision(GameObject other) {
if (other.tag == "Player") {
InitializeIfNeeded ();
float size = 0.0f;
//ParticleSystem.Particle[] points = m_ps.GetParticles(m_Particles);
int numParticlesAlive = m_ps.GetParticles(m_Particles);
ParticleCollisionEvent[] collisions = new ParticleCollisionEvent[numParticlesAlive];
float closestDistanceToIntersect = float.MaxValue;
int closestParticleNumber = -1;
if (numParticlesAlive > 0) {
for (int i = 0; i < numParticlesAlive; i++)
{
Vector3 collisionLocation = collisions [i].intersection;
//find the particle closest to the collision
ParticleSystem.Particle p = m_Particles[i];
float distanceFromIntersect = Vector3.Distance (p.position, collisionLocation);
if (distanceFromIntersect < closestDistanceToIntersect) {
closestDistanceToIntersect = distanceFromIntersect;
closestParticleNumber = i;
}
}
m_Particles [closestParticleNumber].remainingLifetime = -1;
size = m_Particles [closestParticleNumber].startSize;
m_ps.SetParticles (m_Particles, numParticlesAlive);
//Do something here...
Debug.Log("SIZE: " + size);
//other.GetComponent ("Health").addEnergyMana(size);
} else {
Destroy (gameObject);
}
}
}
void InitializeIfNeeded()
{
if (m_ps == null)
m_ps = GetComponent<ParticleSystem>();
if (m_Particles == null || m_Particles.Length < m_ps.main.maxParticles)
m_Particles = new ParticleSystem.Particle[m_ps.main.maxParticles];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment