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.
bool hitting = true;
bool waiting = false;
void Update () {
if (waiting) return;
//Hit with the hammer
if (hitting && transform.rotation.eulerAngles.z > -90) {
transform.Rotate (0, 0, Time.deltaTime * 0.2f, Space.Self);
}
if (hitting && transform.rotation.eulerAngles.z == -90) {
StartCoroutine (HammerHit ());
}
//Raise the hammer
if (!hitting && transform.rotation.eulerAngles.z < 0) {
transform.Rotate (0, 0, -Time.deltaTime * 0.2f, Space.Self);
}
if (!hitting && transform.rotation.eulerAngles.z == 0) {
StartCoroutine (HammerRaised ());
}
}
public IEnumerator HammerHit () {
//Play a hammer hitting sound
EnvironmentManager.instance.PlaySoundAtLocation (25, transform.position);
waiting = true;
yield return WaitScript.instance.pointTwoSecondWait;
hitting = false;
waiting = false;
}
public IEnumerator HammerRaised () {
waiting = true;
yield return WaitScript.instance.pointTwoSecondWait;
hitting = true;
waiting = false;
}
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.
Thanks for the response. After further tinkering I came up with the following that does the job:
bool hit = true;
bool raise = false;
bool hitting = true;
bool raising = false;
void Update () {
if (hit) transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.Euler (0, 0, -90), Time.deltaTime * 300);
if (raise) transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.Euler (0, 0, 0), Time.deltaTime * 300);
if (hitting) StartCoroutine (HammerHit ());
if (raising) StartCoroutine (HammerRaise ());
}
public IEnumerator HammerHit () {
hitting = false;
yield return WaitScript.instance.pointTwoSecondWait;
EnvironmentManager.instance.PlaySoundAtLocation (25, transform.position); //Play a hammer hitting sound
yield return WaitScript.instance.pointTwoSecondWait;
hit = false;
raise = true;
raising = true;
}
public IEnumerator HammerRaise () {
raising = false;
yield return WaitScript.instance.pointTwoSecondWait;
yield return WaitScript.instance.pointTwoSecondWait;
raise = false;
hit = true;
hitting = true;
}
1 Like