So theres a few tutorials out there on doing this but being completely new to programming I cant find a way to integrate it into my game plus I dont want to put all the unnecessary things in I wont be using. Basically I want a shake effect that will last for a few seconds and I will need this only once, I want to activate it as an is trigger, so when you walk up to the object or space the camera/player will shake for a few seconds then never do it again. Does anyone have any simple script I can use?
Here are the steps; - Define variables for max movement for x and y axis. - Define a variable for shaketime. - The shake impact would be normally highest at the start and lowest at the end of the shake time. An example would be; Code (CSharp): IENumerator ShakeCamera(){ float counter = 0f; Vector3 cameraPosition = transform.position; float maxX = 1f; float maxY = 1f; float ShakeTime = 2f; while(true){ counter += Time.deltaTime; if(counter >= ShakeTime){ yield break; } else { transform.position = cameraPosition + new Vector3((ShakeTime - counter) * Random.Range(-maxX, maxX), (ShakeTime - counter) * Random.Range(-maxY, maxY)); } yield return null; } not tested, and you may tweak it to your needs.
Perlin works better than random.range since it isn't as gllitchy looking with the camera instantly moving between values
You could just use iTween, it's free and simple to use tweening library for Unity. Just pop the file in to your project (if it isn't there already as many packages use it.) and then call it from wherever e,g Code (csharp): ShakePosition(GameObject target, Vector3 amount, float time) Only annoying thing about it is that you'll probably need to download it trough asset store as they only link you there. Even tough it's a single file (.cs) that could easily be distributed trough their own site..
iTween is slow as hell. Grab DOTween instead. Way more flexibility, way faster and it's free: http://dotween.demigiant.com/download.php
Obviously the easiest route would be ideal for me as I only jumped into unity about a week ago, could you give me a working example? I took what you said but I cant get it working. kek I am not a smart man
try this maybe: Just attach it to a gameObject. Code (csharp): using UnityEngine; using System.Collections; public class ShakeScript : MonoBehaviour { public float shakeStrength = .1f; public float shakeDecay = 0.005f; float shake_decay; float shake_intensity; Vector3 originPosition; Quaternion originRotation; Transform _transform; //Temporary button for testing void OnGUI() { if (GUI.Button(new Rect(20, 40, 80, 20), "Shake")) { Shake(); } } void OnEnable() { _transform = transform; } IEnumerator ShakeIt() { while(shake_intensity > 0f) { print ("f"); _transform.localPosition = originPosition + Random.insideUnitSphere * shake_intensity; _transform.localRotation = new Quaternion( originRotation.x + Random.Range(-shake_intensity, shake_intensity) * .2f, originRotation.y + Random.Range(-shake_intensity, shake_intensity) * .2f, originRotation.z + Random.Range(-shake_intensity, shake_intensity) * .2f, originRotation.w + Random.Range(-shake_intensity, shake_intensity) * .2f); shake_intensity -= shakeDecay; yield return null; } ShakingStopped(); yield return null; } void ShakingStopped() { _transform.localPosition = originPosition; _transform.localRotation = originRotation; } public void Shake() { originPosition = _transform.localPosition; originRotation = _transform.localRotation; shake_intensity = shakeStrength; StartCoroutine("ShakeIt"); } }