- Home /
Adding a rotation limit to a pointing gun with cursor. I'm losing my mind.
Hi there. I'm working on a 2D platformer game with gun following the cursor for aiming. When the character is flipped, the rotation limit doesn't apply anymore. I'm losing it. See below the pictures. The limit is supposed to be the same of when he faces right. The same applies for the upper limit.
Here's the code
[CODE]
if (_orientation.x < 0)
{
GameState._isCharacterFlipped = true;
_parent.transform.localScale = new Vector3(-1, 1, 1);
if (_shootFX != null)
{
_shootFXTransform.localEulerAngles = new Vector3(0, 180, 0);
}
if (_orientation.y < transform.localPosition.y)
transform.eulerAngles = new Vector3(0, 0, (Mathf.Clamp(tempAngle, -180, 180 + _aimAngleRange/2)) - 180);
//transform.eulerAngles = new Vector3(0, 0, (Mathf.Clamp(tempAngle, -180, 90)));
else
{
transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp(tempAngle, -180 - _aimAngleRange / 2, 180) - 180);
//transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp(tempAngle, 90, 180));
}
}
else
{
GameState._isCharacterFlipped = false;
if (_shootFX != null)
_shootFXTransform.localEulerAngles = new Vector3(0, 0, 0);
_parent.transform.localScale = new Vector3(1, 1, 1);
transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp(tempAngle, -_aimAngleRange / 2, _aimAngleRange / 2));
}
[/CODE]
I've lost so many days on this I really don't know what to do anymore. Please help me.
Thanks
Answer by alexianphilosophy · Feb 04 at 05:34 PM
You can try and have two sets of clamping variables: one for when your character isn't flipped and one for when it is. I've worked with this type of rotation issue before, it's quite annoying. Another option is to flip the visuals of the player and manage the gun on its own.
Also, think of a 360 degree circle. It looks like you're trying to clamp the shooting from -180 degrees to 180 + some amount. 180 degrees and -180 degrees are the same thing. You're overshooting the 180 degrees, but it looks like would would have almost full range of motion in the first place. Maybe you're trying to clamp so the can can only face "forward"? That would look like a clamp from 90 to -90 degrees.
Try the two sets of variables for a start, probably. You can do that in tandem to making the sprite and the weapon flip individually as well to make it simpler. Also check out your range because it might just be that your range is a full circle and you don't realize it.
Hope this helped.
Your answer
Follow this Question
Related Questions
Flip the player with arm rotation 2 Answers
Sprite will not roate when arrow keys are pressed 2 Answers
2D Sprite constantly flipping 0 Answers
gimble lock and rotation problem 2 Answers