Search Unity

Using rotation to lower and raise a hammer sprite

Discussion in 'Scripting' started by QuinnWinters, Jul 24, 2020.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I'm trying to make a hammer sprite lower and raise using rotation. What am I doing wrong here? I get no rotation at all.

    Code (CSharp):
    1. bool hitting = true;
    2. bool waiting = false;
    3.  
    4. void Update () {
    5.  
    6.     if (waiting) return;
    7.  
    8.     //Hit with the hammer
    9.     if (hitting && transform.rotation.eulerAngles.z > -90) {
    10.         transform.Rotate (0, 0, Time.deltaTime * 0.2f, Space.Self);
    11.     }
    12.     if (hitting && transform.rotation.eulerAngles.z == -90) {
    13.         StartCoroutine (HammerHit ());
    14.     }
    15.  
    16.     //Raise the hammer
    17.     if (!hitting && transform.rotation.eulerAngles.z < 0) {
    18.         transform.Rotate (0, 0, -Time.deltaTime * 0.2f, Space.Self);
    19.     }
    20.     if (!hitting && transform.rotation.eulerAngles.z == 0) {
    21.         StartCoroutine (HammerRaised ());
    22.     }
    23.  
    24. }
    25.  
    26. public IEnumerator HammerHit () {
    27.  
    28.     //Play a hammer hitting sound
    29.     EnvironmentManager.instance.PlaySoundAtLocation (25, transform.position);
    30.  
    31.     waiting = true;
    32.     yield return WaitScript.instance.pointTwoSecondWait;
    33.     hitting = false;
    34.     waiting = false;
    35.  
    36. }
    37.  
    38. public IEnumerator HammerRaised () {
    39.  
    40.     waiting = true;
    41.     yield return WaitScript.instance.pointTwoSecondWait;
    42.     hitting = true;
    43.     waiting = false;
    44.  
    45. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Stick some
    Debug.Log() 
    calls in here and there, one even before the
    if(waiting)return;
    construct, see if any of the code is even running.

    If you just have a lift/drop from 0 to 90 and back to 0, you could probably get better control with a simple
    public AnimationCurve 
    property in your script where you define the shape of the curve, then just evaluate the curve over time and use that to pivot your arm up 90 degrees, then down. Far less math involved, just on call to making a Quaternion out of the final result.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Thanks for the response. After further tinkering I came up with the following that does the job:

    Code (CSharp):
    1. bool hit = true;
    2. bool raise = false;
    3. bool hitting = true;
    4. bool raising = false;
    5.  
    6. void Update () {
    7.  
    8.     if (hit) transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.Euler (0, 0, -90), Time.deltaTime * 300);
    9.     if (raise) transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.Euler (0, 0, 0), Time.deltaTime * 300);
    10.  
    11.     if (hitting) StartCoroutine (HammerHit ());
    12.     if (raising) StartCoroutine (HammerRaise ());
    13.  
    14. }
    15.  
    16. public IEnumerator HammerHit () {
    17.  
    18.     hitting = false;
    19.     yield return WaitScript.instance.pointTwoSecondWait;
    20.     EnvironmentManager.instance.PlaySoundAtLocation (25, transform.position); //Play a hammer hitting sound
    21.     yield return WaitScript.instance.pointTwoSecondWait;
    22.     hit = false;
    23.     raise = true;
    24.     raising = true;
    25.  
    26. }
    27.  
    28. public IEnumerator HammerRaise () {
    29.  
    30.     raising = false;
    31.     yield return WaitScript.instance.pointTwoSecondWait;
    32.     yield return WaitScript.instance.pointTwoSecondWait;
    33.     raise = false;
    34.     hit = true;
    35.     hitting = true;
    36.  
    37. }
     
    Kurt-Dekker likes this.