Quantcast
Viewing all articles
Browse latest Browse all 59

Answer by rabbitfang

You are right in that the rotation will never hit zero exactly, due to float values being used. You also have the issue of using `transform.Rotate.y` which should give you an error but does not for some reason (it should be transform.[localEulerAngles][1].y). You also need to prevent the barrel from being rotated too far. Here is a rewritted (untested) function for you (I also took the liberty of making it move in the best direction to get back to 0 the fastest). function resetBarrel(){ print("resetting barrel"); var epsilon : float = 0.001f; var currentAngle : float = transform.localEulerAngles.y; if (currentAngle < 360 - epsilon && currentAngle > epsilon) { print("barrel isnt at 0"); var angleDelta : float = Time.deltaTime * 40; // positive if forward; negative if backward (rotation wise) var rotMultiplier : int = (currentAngle > 180) ? 1 : -1; var resultAngle : float = Mathf.Clamp(currentAngle + rotMultiplier * angleDelta, 0, 360); var rotation : Vector3 = transform.localEulerAngles; rotation.y = resultAngle; transform.localEulerAngles = rotation; } else { print("barrel at 0"); } } Again, this is untested so there might be some lurking syntax/logic errors in here. The `(currentAngle > 180) ? 1 : -1` part is so it rotates in the best direction (to get to 0 the fastest). If you don't want that behavior, get rid of the variable (and remove the reference to it in the `Mathf.Clamp` line). [1]: http://unity3d.com/support/documentation/ScriptReference/Transform-localEulerAngles.html

Viewing all articles
Browse latest Browse all 59

Trending Articles