- Home /
Question by
UnityMohammed · Jun 04, 2018 at 03:45 PM ·
camerascripting problemcollision
How do I get my camera shaker to trigger on collisions?
Hello All! I am trying to get camera shaker script to trigger when ever the player hits a certain tagged game object. I am not sure how to go about it.
using UnityEngine; using System.Collections;
public class CollisionShake : MonoBehaviour
{
public cameraShake shaker;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Obstacle")
{
Shake();
}
}
}
Here is my camera shaker script for context. (the test button works well)
using UnityEngine; using System.Collections; public class cameraShake : MonoBehaviour { private Vector3 originPosition; private Quaternion originRotation; public float shake_decay; public float shake_intensity;
void OnGUI()
{
if (GUI.Button(new Rect(20, 40, 80, 20), "Shake"))
{
Shake();
}
}
void Update()
{
if (shake_intensity > 0)
{
transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
transform.rotation = new Quaternion(
originRotation.x + Random.Range(-shake_intensity, shake_intensity) * .1f,
originRotation.y + Random.Range(-shake_intensity, shake_intensity) * .1f,
originRotation.z + Random.Range(-shake_intensity, shake_intensity) * .1f,
originRotation.w + Random.Range(-shake_intensity, shake_intensity) * .1f);
shake_intensity -= shake_decay;
}
}
void Shake()
{
originPosition = transform.position;
originRotation = transform.rotation;
shake_intensity = .3f;
shake_decay = 0.03f;
}
}
Comment