Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Small sniper optics shaking

Discussion in 'Scripting' started by Mirek Uhlir, Dec 28, 2010.

  1. Mirek Uhlir

    Mirek Uhlir

    Joined:
    Dec 25, 2009
    Posts:
    124
    I would like to simulate optics shaking. I thought something like that.

    Code (csharp):
    1. function Update() {
    2.  
    3.  
    4. if(focus == true){
    5.        
    6.            
    7.             StartCoroutine("Shake");   
    8.                                                                                        
    9.             mc.transform.Rotate(0, movement * Time.deltaTime, 0);
    10.  
    11. }
    12.  
    13.  
    14. function Shake(){
    15.  
    16.  
    17.  
    18. movement = -5;
    19.  
    20. yield WaitForSeconds(1);
    21.  
    22.  
    23. movement = 5;
    24. yield WaitForSeconds(1);
    25.  
    26.  
    27. return;
    28. }
    29.  
    30.  
    31.  
    Doesn´t work. Thanks for any advice.
     
  2. sangi93

    sangi93

    Joined:
    Aug 4, 2010
    Posts:
    77
    You're starting coroutine every update call, you can try starting it once when you get focus, and stopping once you lose it.
     
  3. Mirek Uhlir

    Mirek Uhlir

    Joined:
    Dec 25, 2009
    Posts:
    124
    I am not sure if I use return and StartCoroutine in right way. Fow example in Delphi it is easy, function can call itself. I need repeating function.
     
  4. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    Try using InvokeRepeating, you shouldn't even need the Update Function then.
     
  5. devans770

    devans770

    Joined:
    Nov 25, 2010
    Posts:
    45
    or rather than using a co-routine or callback function why not use some good old fashion math. the basic model you described could be written as such

    Code (csharp):
    1.  
    2. var BreathDuration : float = 2.0;
    3. var BreathApplitude : float = 0.5;
    4.  
    5. function Update() {
    6.  
    7.  var PingPongValue : float = Mathf.PingPong(Time.time,BreathDuration)/BreathDuration;
    8.  var LerpValue : System.Single = Mathf.Lerp(-BreathDuration,BreathDuration,PingPongValue);
    9.  transform.Rotate( LerpValue,0 , 0);
    10.  
    11. }
    12.  
    I would extend this model to include some random vector movements to provide some lateral movement. You could also isolate the breath movement on a key press to simulate holding breath.

    Another reason you may not be getting results is if you already have a script modifying the camera rotation.. in which case you would need to combine the rotation in to a single script. you can test this out by disabling other scripts such as mouse look during runtime.
     
  6. chiblue3D

    chiblue3D

    Joined:
    Apr 25, 2012
    Posts:
    20
    Thanks for this code, but I notice that you have a BreathApplitude variable defined but this is not used in the code, is this correct?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    It looks like it should be getting used as a multiplier on the LerpValue in the last line.