Created
June 28, 2018 07:57
-
-
Save walkies/94549f22608abbdff6c8af9cd0aeb9c5 to your computer and use it in GitHub Desktop.
CollisionBehaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CollisionBehaviour : MonoBehaviour { | |
[Header("Stats")] | |
[Tooltip("Force applied to gameobject after a collision.")] | |
public float BounceSpeed = 300; | |
[Tooltip("Damage dealt to another gameobject on collision if other object has a TakeDamage(int) function.")] | |
public int damageToOther = 1; | |
[Header("Behaviour List")] | |
[Tooltip("List of actions taken when this gameobject collides with another.")] | |
[SerializeField] | |
private CollisionBehaviourData[] behaviours; | |
private Rigidbody rb; | |
void Start () { | |
rb = GetComponent<Rigidbody>(); | |
} | |
void OnCollisionEnter(Collision c) | |
{ | |
for (int i = 0; i < behaviours.Length; i++) | |
{ | |
if (c.gameObject.CompareTag(behaviours[i].tag)) | |
{ | |
behaviours[i].col = c; | |
if(behaviours[i].effectsToPlay != null) | |
{ | |
if (!string.IsNullOrEmpty(behaviours[i].effectsToPlay.soundEffectName)) | |
{ | |
PlaySound(behaviours[i].effectsToPlay.soundEffectName); | |
} | |
if (!string.IsNullOrEmpty(behaviours[i].effectsToPlay.particleEffectName)) | |
{ | |
PlayParticleEffect(behaviours[i].effectsToPlay.particleEffectName); | |
} | |
} | |
if (behaviours[i].bounceThis) | |
{ | |
Vector3 dir = c.contacts[0].point - transform.position; | |
dir = -dir.normalized; | |
rb.AddForce(dir * BounceSpeed / 4); | |
} | |
if (behaviours[i].destroyThis) | |
{ | |
Destroy(); | |
} | |
c.gameObject.SendMessage("TakeDamage", damageToOther, SendMessageOptions.DontRequireReceiver); // change later | |
} | |
} | |
} | |
public void PlaySound(string soundToPlay) | |
{ | |
if (string.IsNullOrEmpty(soundToPlay)) | |
{ | |
Debug.LogWarning("No sound effect name entered."); | |
return; | |
} | |
else | |
{ | |
AudioManager.instance.PlaySound(soundToPlay); | |
} | |
} | |
public void PlayParticleEffect(string effectToPlay) | |
{ | |
if (string.IsNullOrEmpty(effectToPlay)) | |
{ | |
Debug.LogWarning("No particle effect name entered."); | |
return; | |
} | |
else | |
{ | |
ParticleManager.instance.PlayParticleEffect(effectToPlay,transform); | |
} | |
} | |
public void Destroy() | |
{ | |
BallDestroyedData ballDestroyedData = new BallDestroyedData(); | |
//ballDestroyedData variables here | |
GlobalEvents.OnBallDestroyed.Invoke(ballDestroyedData); | |
//Sound or particle effect here. | |
Destroy(gameObject); | |
} | |
public void DealDamage() | |
{ | |
Debug.Log("Do damage"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment