- Home /
Limit on x rotation axis in play mode
Hello, I am trying to rotate an object toward a target. I have achieved this on the Y rotation axis and trying to do the same for the X rotation axis. This is the code for rotating toward a target angle:
diffAngle = Mathf.DeltaAngle(currentAngle, targetAngle);
turnDirection = Mathf.Clamp(diffAngle / 20.0f, -1.0f, 1.0f);
It starts fine, rotating towards the target, but the rotation is clamped. Starting from 0 degrees, the object cannot rotate any further than 270 or 90 degrees. I tried adjusting the values in the editor, though I cannot drag the value beyond these limits. I tried adding a new Cube object, which does the same, so I have not coded anything to create this limit. When I stop the game the limitation is removed and I can rotate to any value I want.
I'm not sure why and would appreciate help in solving this problem.
I'm not sure if I understand this correctly, but are you asking why your script limits are not enforced when the game isn't playing. If so, scripts are generally not executed in the editor. To execute them in the editor, you have to add a flag [ExecuteInEdit$$anonymous$$ode] to the script. See http://unity3d.com/support/documentation/ScriptReference/ExecuteInEdit$$anonymous$$ode.html
No pal. I have not set up any limits, but it limits the X rotation. I do not want any limits.
Answer by Owen-Reynolds · Apr 14, 2012 at 05:05 PM
That's a small code fragment. There's probably something messed up in the code that actually does the turning (which we can't see.) It looks like turnDirection
is set up to go full-speed if past 20 degrees, then slow down within 20. Looks fine. You might make it a public so you can double-check it.
The Inspector is nothing. Whatever bug effectively limits rotation, naturally "snaps back" Inspector changes also.
Answer by centaurianmudpig · Apr 14, 2012 at 06:25 PM
Yeah that's right, about slowing down within 20. Here is a small [link text]unity project[1] reproducing the problem. It looks like DeltaAngle() returns a negative value after the initial 90 degree rotation, I don't understand, hopefully this example will say more.
[1]: http://answers.unity3d.com/storage/temp/633-RotateToTarget.zip
The rectangle (Hunter) should rotate to face the sphere (Prey).