- Home /
2D GunPivot Bug
In my game i use joystick axis X to move around and joystick axis 3 and 4 to rotate the gun around. The 3rd axis also flips the player. The clamp just prevents it from going too high for each player class. Everything works fine until i flip the player. Then the gun is either at the top of the clamp when 4th axis is greater than 0, bottom when it's less than 0, or in the middle when it's at 0. Here's the code :
using UnityEngine;
using System.Collections;
public class GunMove : MonoBehaviour
{
public int player = 0;
string xAxis = "X P1";
string yAxis = "Y P1";
public float clamp;
public bool isFacingRight=true;
void Update ()
{
float x = Input.GetAxis(xAxis);
float y = Input.GetAxis(yAxis);
float angle = Mathf.Atan2(y,x) * Mathf.Rad2Deg;
angle = Mathf.Clamp(angle, -clamp, clamp);
transform.localEulerAngles = new Vector3(0, 0, angle);
if(x > 0 && !isFacingRight)
{
Flip();
}
if(x < 0 && isFacingRight)
{
Flip();
}
}
void Flip()
{
Vector2 parentScale = transform.parent.localScale;
parentScale.x *= -1;
transform.parent.localScale = parentScale;
isFacingRight = !isFacingRight;
}
Here's a video of the game bug
Thanks for any help
Answer by Bentley · Jan 13, 2015 at 01:28 AM
Negative scaling to flip sprites can cause weird issues. Try:
parent.transform.localEulerAngles = new Vector3(0, 180);
Instead of:
parentScale.x *= -1;
Hope this helps, Bentley
Your answer
Follow this Question
Related Questions
Scaling my background Sprite to fill screen? (2d) 2 Answers
Moving camera by z axis in 2D 1 Answer
doubt with pixels per unit 1 Answer
2D C# top down shooter, adjusting the projectiles angle 1 Answer
Issues calculating angle in 2D 0 Answers