Thursday, August 25, 2022
HomeGame Developmentunity - Clamping angle between two ranges

unity – Clamping angle between two ranges


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

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

enter image description here

The “Participant” incorporates a Rigidbody2D answerable for the motion. The “Physique” is an animated sprite that (thus far) strikes alongside the Participant entity. So it is ready to transfer the sprite across the “full circle”.

enter image description here

My fundamental downside is, that after it crosses the y-Axis (z-Axis rotation > Abs(90)), the “Physique” is the other way up which is admittedly wanting bizarre. Moreover, it the “model” of my recreation does probably not assist the participant “visually” going immediately upward or downward the y-Axis. That is why I need 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. Restrict the visible (simply the visible) illustration of the participant’s going through course – see picture beneath purple space – so the participant isn’t proven going immediately up or down (the Rigidbody2D nonetheless can transfer on this course, it is simply not visualized that manner).

enter image description here

My present rotation logic is sort of easy:

var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
var angular = Mathf.LerpAngle(remodel.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 course. I am additionally capable of “counter-rotate” the “Physique” element contained in the “Participant” element in order that it at all times appears in the identical course (unaffected by the Rigidbody motion) with this:

var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
var angular = Mathf.LerpAngle(remodel.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.remodel.rotation.z * 1f);
this._bodyTransform.rotation = counterDirection;

However no matter I attempt to flip the Physique on the y-Axis, doesn’t work and solely rotated by 90 levels as a substitute of the complete 180. Moreover, I’ve fairly some issues determining easy methods to accurately “clamp” the rotation between -45 < rotation < 45 and 135 < rotation < 225 as a result of the rotation may very well be both within the constructive or damaging rotation illustration.

Any recommendations on easy methods to remedy this is able to be extremely appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments