Search Unity

Question Anyone have tips on making an item shake after a wrong guess?

Discussion in 'Scripting' started by starpoxs, Dec 10, 2022.

  1. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    Basically I have a cube item in my scene, and another script tracking correct and incorrect guesses. Been following a lot of guides to get to this point, so I feel kind of in the dark.

    Anyway, I want the cube to shake each time the player inputs an incorrect guess. Anyone got tips on how to do that? Where should I start looking?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Just animate it and play the animation.
     
    Reedex and starpoxs like this.
  3. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    Do you know how to do this across scripts? Basically:
    Code (csharp):
    1.  
    2. public void Check()
    3.     {
    4.        if
    5.         {
    6.             OnWordCheckCorrect?.Invoke();
    7.             _inputField.text = "";
    8.          //    isleShake.SetTrigger("TrShake")
    9.         }
    10.     }
    11.  
    the committed out bit didn't work. here is the script attached to the shaking item.

    Code (csharp):
    1.  
    2.  public Animator isleShake;
    3.     void Start()
    4.     {
    5.         isleShake = GetComponent<Animator>();
    6.     }
    7.     void Update()
    8.     {
    9.     }
    10.  
    the script and animation are attached to the shaking item, the thing that should trigger the shaking is elsewhere (i can't attach it to the shaking item, not without a lot of code restructuring -- hoping to avoid that)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is not great for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  5. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    Thanks. But, I am now getting a "Object reference not set to an instance of an object" error.
    Code (csharp):
    1.  
    2. // public PenaltyIsle shakeIt;
    3. .
    4. .
    5. .
    6. public void Check()
    7.     {
    8.        if
    9.         {
    10.             OnWordCheckCorrect?.Invoke();
    11.             _inputField.text = "";
    12.              // shakeIt.Shake();
    13.         }
    14.     }
    15.  
    Code (csharp):
    1.  
    2. public class PenaltyIsle : MonoBehaviour
    3. {
    4.     public Animator isleShake;
    5.     void Start()
    6.     {
    7.         isleShake = GetComponent<Animator>();
    8.     }
    9.   //  public void Shake()
    10.   //  {
    11.  //       isleShake.SetTrigger("TrShake");
    12.  //   }
    13. }
    14.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    starpoxs likes this.
  7. starpoxs

    starpoxs

    Joined:
    Nov 30, 2022
    Posts:
    6
    NEVERMIND I got it figured out. Thanks to all.
     
    Last edited: Dec 11, 2022