Home > other >  For help, the object will return after the rotation, how to reduce the rotation Angle or locked?
For help, the object will return after the rotation, how to reduce the rotation Angle or locked?

Time:11-21

When they study the unity rotating object found a problem:

When press W, object forward, rotating Y is 0,

When holding the D, the object toward the right, rotating the Y axis is 90,


While at the same time hold the W and D object toward the left 45 degrees, rotating the Y axis is 45 degrees,


When let go, there is an error, the object will be rotary, Angle will not remain in the 45 degrees (if not set smooth rotating object can be instantly back to 0 degrees or 90 degrees, error performance obvious)


The reason for the error seems to be:
People generally difficult to instant loosen the two buttons at the same time, such as loose D button was 0.1 seconds slower than loose W button, the objects in an instant from 45 degrees to 90 degrees,
If set up a smooth rotation and rotating speed, the smaller the rotation speed rotary speed slower, if also loosen the fast objects may return to 40 or 50 degrees even smaller, rotary not clearly, if loosen slowly, sometimes turning over 10 degrees is very obvious;
Another problem is that if the speed is too small, rotary is not obvious, but the object turn round and then will be very slow, the control will be very inflexible,
Another problem is that W + D and W and overlapping D button


Contrast found that most of the game now (including single online mobile game, etc.) do not turn, loosen the remains after 45 degree Angle, Angle like locked, instant 2 button put slow, also won't turn, but does not affect W and D
Many game: W, A, S, D character forward, backward, left, right, is there are A lot of instant transfer), general and two adjacent keys as W D, characters will toward the left

Later found some relatively early online, let go after two buttons, characters will moment is back, keep forward


How help bosses like most games even loosen slowly, objects also remain in the Angle of the right front or locking Angle? Thank you


The code is shown in figure:












//rotation speed
Public float speed=19;
//vector
Public Vector3 v3=Vector3. Zero;


Float h=Input. GetAxis (" Horizontal ");
Float v=Input. GetAxis (" Vertical ");
V3=new Vector3 (h, 0 v);
V3. The Normalize ();
If (h!=0 | | v!=0)
{
//towards
Quaternion a=Quaternion. LookRotation (v3);


//smooth rotation
Quaternion a1=Quaternion. Lerp (transform rotation, a, speed * Time deltaTime);
The transform. The rotation=a1;



////there's no need to smooth rotation, when the W and D release button, the object will be an instant return to 0 degrees or 90 degrees
//Quaternion a=Quaternion. LookRotation (v3);
//the transform. The rotation=a;
  • Related