x


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.

more ▼

asked Apr 13 '12 at 05:09 PM

centaurianmudpig gravatar image

centaurianmudpig
41 9 11 12

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 [ExecuteInEditMode] to the script. See http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html

Apr 13 '12 at 06:10 PM dkNinja

No pal. I have not set up any limits, but it limits the X rotation. I do not want any limits.

Apr 13 '12 at 09:34 PM centaurianmudpig
(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

Yeah that's right, about slowing down within 20. Here is a small [link text]unity project1 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.

The rectangle (Hunter) should rotate to face the sphere (Prey).

RotateToTarget.zip (214.7 kB)
more ▼

answered Apr 14 '12 at 06:25 PM

centaurianmudpig gravatar image

centaurianmudpig
41 9 11 12

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 14 '12 at 05:05 PM

Owen Reynolds gravatar image

Owen Reynolds
12.2k 1 7 46

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2243
x1728
x230
x137
x38

asked: Apr 13 '12 at 05:09 PM

Seen: 582 times

Last Updated: Apr 14 '12 at 06:25 PM