Skip to content

Instantly share code, notes, and snippets.

@k-harris k-harris/ExplosionForce.cs Secret
Created Feb 9, 2019

Embed
What would you like to do?
Caster Arts Effects. This set includes application of radial force, rotating an object in place, and rising out of the ground.
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2017 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplosionForce : MonoBehaviour
{
public bool repeating = false;
public float waitBtwnPulses = 0.5f;
public float waitVariance = 0.1f;
public float radius = 5.0F;
public float radiusVariance = 0F;
public float power = 10.0F;
public float powerVariance = 0F;
public float cameraShakeTime = 0f;
public float casmeraShakeAmount = 1f;
private Coroutine worldShaking = null;
public bool playerRedFilter = false;
void Start()
{
ApplyExplosionForce();
if (repeating)
StartCoroutine(ExplosionPulse());
}
private void Update()
{
/*
if (Globals.testingModeOn && Input.GetKeyUp(KeyCode.M))
{
ApplyExplosionForce();
}*/
}
public IEnumerator ExplosionPulse()
{
ApplyExplosionForce();
yield return new WaitForSeconds(Random.Range(waitBtwnPulses - waitVariance, waitBtwnPulses + waitVariance));
StartCoroutine(ExplosionPulse());
}
public void ApplyExplosionForce()
{
if (playerRedFilter)
GameObject.FindGameObjectWithTag("Player").GetComponent<FlashFilter>().Fade();
Vector3 explosionPos = transform.position;
float rad = Random.Range(radius - radiusVariance, radius + radiusVariance);
Collider[] colliders = Physics.OverlapSphere(explosionPos, rad);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb != null)
{
float pow = Random.Range(power - powerVariance, power + powerVariance);
rb.AddExplosionForce(pow, explosionPos, rad, 3.0F);
}
//shake player camera
if (worldShaking != null)
StopCoroutine(worldShaking);
worldShaking = StartCoroutine(CameraShake());
}
}
public void ThrowObject()
{
Vector3 explosionPos = transform.position;
float rad = Random.Range(radius - radiusVariance, radius + radiusVariance);
Collider[] colliders = Physics.OverlapSphere(explosionPos, rad);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb != null)
{
float pow = Random.Range(power - powerVariance, power + powerVariance);
rb.AddExplosionForce(pow, explosionPos, rad, 3.0F);
}
//shake player camera
if (worldShaking != null)
StopCoroutine(worldShaking);
worldShaking = StartCoroutine(CameraShake());
}
}
public float detectionDistance = 10f;
public IEnumerator CameraShake()
{
GameObject player = GameObject.FindGameObjectWithTag("MainCamera").gameObject;
CameraShake shake = player.GetComponent<CameraShake>();
float dist = Vector3.Distance(player.transform.position, gameObject.transform.position);
if (dist < detectionDistance)
{
if (shake != null)
{
shake.enabled = true;
if (cameraShakeTime != 0)
shake.ShakeCamera(casmeraShakeAmount, cameraShakeTime);
}
}
yield return null;
}
}
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2017 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRotation : MonoBehaviour
{
Transform _transform;
[SerializeField]
Vector3 axisOfRotation = new Vector3(0,1,0);
[SerializeField]
float speed = 3f;
[SerializeField]
float speedModifier = 1f;
float speedMod = 1f;
void Awake()
{
_transform = GetComponent<Transform>();
}
// Update is called once per frame
void Update ()
{
speedMod += speedModifier / 100;
_transform.Rotate(axisOfRotation * Time.deltaTime * (speed * 10) * speedMod);
}
}
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2017 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectTranslation : MonoBehaviour
{
public bool startOnAwake = false;
[SerializeField]
Vector3 startingPosition = new Vector3(0, 0, 0);
[SerializeField]
Vector3 endingPosition = new Vector3(0, 0, 0);
[SerializeField]
float translationTime = 3;
[SerializeField]
bool destroyObject = false;
[SerializeField]
float waitTime = 10f;
private void Awake()
{
gameObject.transform.localPosition = startingPosition;
}
// Use this for initialization
void Start ()
{
if (startOnAwake)
StartCoroutine(RiseAndReverse(endingPosition, startingPosition));
}
public IEnumerator TranslateObject(Vector3 _newPos, float _translationTime)
{
for (float t = 0; t < 1; t += Time.deltaTime / _translationTime)
{
gameObject.transform.localPosition = Vector3.Lerp(gameObject.transform.localPosition, _newPos, t);
yield return null;
}
}
public IEnumerator RiseAndReverse(Vector3 _newPos1, Vector3 _newPos2)
{
StartCoroutine(TranslateObject(endingPosition, translationTime));
yield return new WaitForSeconds(waitTime);
if (destroyObject)
{
StartCoroutine(TranslateObject(startingPosition, (translationTime * 3)));
Destroy(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.