简体   繁体   中英

My GameObject's transform won't rotate through 360 - “pingpongs” between 270 - 90. (Unity 3D C#)

Sorry in advance if this is difficult to follow, I'll draw up diagrams if need be.

I'm using Unity3D to develop a game and am having trouble with the player's transform. The game is first person, 3D, and there are points where the gravity will change, allowing the player to walk along a wall - This is all up and running.

When the players gravity is such that it walks on a wall, when I rotate the character's to look around its transform behaves strangely. It can rotate through a full circle and there are no visible problems with the character when playing the game.

If I look at the inspector in Play Mode, let's say I'm continually turning right, the following happens:

  • the rotation increases up to 90 degrees
  • as soon as it reaches 90 degrees, it begins to count in reverse back down towards 0, as though I have changed direction (but I am still turning right)
  • it then counts down past 0 and continues to 270 - once it hits 270 it begins counting back up again, increasing back up to 90, and so on. The other axes change now and then, only by 0.00001 degree, but they stay around the value they should (the same thing happens on the ground and that rotates fine, so I don't think this is the problem).

The sequence would go something like: 0, 1, 2 ... 88, 89, 90, 89, 88 ...1, 0, 359, ...271, 270, 271 ... 359, 0, 1 ...89, 90, 89 ... etc.

When I watch the transform in the Scene view, it goes back and forth like a wiper on a car window, when but my character itself is just continually turning in one direction. When I am on the ground it makes full circles as it should, even though the value is still "ping-ponging".

What causes this to happen? It doesn't happen when the player character is on the floor, only when it is on walls. The reason I need to know is because there is something else in the game that relies on mimicking the player's rotation, and this problem is giving me behaviour that I don't want.

Any help would be greatly appreciated, I'd had trouble with this for a while!

You shouldn't rely on angle values in Unity, it uses quaternions for rotation:

https://unity3d.com/ru/learn/tutorials/modules/intermediate/scripting/quaternions http://docs.unity3d.com/ScriptReference/Quaternion.html

Strange behaviour came up from the fact, that multiple combinations of angle values are representing the same rotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM