Search Unity

Shake Effect C# help

Discussion in 'Scripting' started by mikuru15, Nov 24, 2014.

  1. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    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?
     
  2. koray1396

    koray1396

    Joined:
    Mar 18, 2014
    Posts:
    10
    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):
    1.  
    2. IENumerator ShakeCamera(){
    3. float counter = 0f;
    4. Vector3 cameraPosition = transform.position;
    5. float maxX = 1f;
    6. float maxY = 1f;
    7. float ShakeTime = 2f;
    8. while(true){
    9.     counter += Time.deltaTime;
    10.     if(counter >= ShakeTime){
    11.         yield break;
    12.     } else {
    13.         transform.position = cameraPosition + new Vector3((ShakeTime - counter) *         Random.Range(-maxX, maxX), (ShakeTime - counter) * Random.Range(-maxY, maxY));
    14.     }
    15. yield return null;
    16. }
    not tested, and you may tweak it to your needs.
     
  3. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Perlin works better than random.range since it isn't as gllitchy looking with the camera instantly moving between values
     
  4. koray1396

    koray1396

    Joined:
    Mar 18, 2014
    Posts:
    10
    yea, maybe... that depends on what you need, I like the sudden explosion like effect.
     
  5. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    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):
    1. 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..
     
  6. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    ericbegue likes this.
  7. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    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
     
  8. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    try this maybe:
    Just attach it to a gameObject.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ShakeScript : MonoBehaviour
    6. {
    7.     public float shakeStrength = .1f;
    8.     public float shakeDecay = 0.005f;
    9.  
    10.     float shake_decay;
    11.     float shake_intensity;
    12.  
    13.     Vector3 originPosition;
    14.     Quaternion originRotation;
    15.     Transform _transform;
    16.  
    17.     //Temporary button for testing
    18.     void OnGUI()
    19.     {
    20.         if (GUI.Button(new Rect(20, 40, 80, 20), "Shake"))
    21.         {
    22.             Shake();
    23.         }
    24.     }
    25.  
    26.     void OnEnable()
    27.     {
    28.         _transform = transform;
    29.     }
    30.  
    31.     IEnumerator ShakeIt()
    32.     {
    33.         while(shake_intensity > 0f)
    34.         {
    35.             print ("f");
    36.             _transform.localPosition = originPosition + Random.insideUnitSphere * shake_intensity;
    37.             _transform.localRotation = new Quaternion(
    38.                 originRotation.x + Random.Range(-shake_intensity, shake_intensity) * .2f,
    39.                 originRotation.y + Random.Range(-shake_intensity, shake_intensity) * .2f,
    40.                 originRotation.z + Random.Range(-shake_intensity, shake_intensity) * .2f,
    41.                 originRotation.w + Random.Range(-shake_intensity, shake_intensity) * .2f);
    42.             shake_intensity -= shakeDecay;
    43.             yield return null;
    44.         }
    45.  
    46.         ShakingStopped();
    47.  
    48.         yield return null;
    49.     }
    50.  
    51.     void ShakingStopped()
    52.     {
    53.         _transform.localPosition = originPosition;
    54.         _transform.localRotation = originRotation;
    55.     }
    56.  
    57.     public void Shake()
    58.     {
    59.         originPosition = _transform.localPosition;
    60.         originRotation = _transform.localRotation;
    61.  
    62.         shake_intensity = shakeStrength;
    63.         StartCoroutine("ShakeIt");
    64.     }
    65. }
    66.  
     
    KarlKarl2000 likes this.
  9. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    Awesome this'l do the job I reckon, thanks again guys
     
  10. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Thanks Sluice! that did it
     
    sluice likes this.
  11. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    +1