Home Game Development unity – Clamping angle between two ranges

unity – Clamping angle between two ranges

0
unity – Clamping angle between two ranges

[ad_1]

I attempt to “restrict” the quantity of angular motion my character is ready to “present”. I’m utilizing Unity.

I’ve 3 parts in my participant entity as beneath:

enter image description here

The “Player” incorporates a Rigidbody2D accountable for the motion. The “Body” is an animated sprite that (to date) strikes alongside the Player entity. So it is ready to transfer the sprite across the “full circle”.

enter image description here

My fundamental drawback is, that when it crosses the y-Axis (z-Axis rotation > Abs(90)), the “Body” is the wrong way up which is admittedly trying bizarre. Furthermore, it the “model” of my sport does probably not assist the participant “visually” going instantly upward or downward the y-Axis. That’s why I wish to do 2 issues:

  1. “Flip” the “physique” (mirror on the y-Axis) as soon as the z-Axis rotation is above/beneath 90.
  2. Limit the visible (simply the visible) illustration of the participant’s dealing with route – see picture beneath purple space – so the participant is rarely proven going instantly up or down (the Rigidbody2D nonetheless can transfer on this route, it is simply not visualized that manner).

enter image description here

My present rotation logic is kind of easy:

var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
var angular = Mathf.LerpAngle(rework.rotation.eulerAngles.z, angle, this._behaviorBase.MaxAngularAcceleration * Time.deltaTime);
angular = (angular + DEG_IN_2PI) % DEG_IN_2PI;
var angularDirection = Quaternion.Euler(0f, 0f, angular);
this._body.SetRotation(angularDirection);

This simply rotates the participant’s Rigidbody2D into the motion route. I’m additionally in a position to “counter-rotate” the “Body” element contained in the “Player” element in order that it all the time seems in the identical route (unaffected by the Rigidbody motion) with this:

var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
var angular = Mathf.LerpAngle(rework.rotation.eulerAngles.z, angle, 
this._behaviorBase.MaxAngularAcceleration * Time.deltaTime);
angular = (angular + DEG_IN_2PI) % DEG_IN_2PI;
var angularDirection = Quaternion.Euler(0f, 0f, angular);
this._body.SetRotation(angularDirection);
// new code for counter rotation
var counterDirection = Quaternion.Euler(0f, 0f, this._body.rework.rotation.z * 1f);
this._bodyTransform.rotation = counterDirection;

But no matter I attempt to flip the Body on the y-Axis, doesn’t work and solely rotated by 90 levels as a substitute of the total 180. Furthermore, I’ve fairly some issues determining easy methods to appropriately “clamp” the rotation between -45 < rotation < 45 and 135 < rotation < 225 as a result of the rotation might be both within the constructive or unfavorable rotation illustration.

Any tips about easy methods to remedy this might be extremely appreciated.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here