Hello, in the code below i am trying to get my character to move while keeping its rotation same. I have a rotation command at the top of “FixedUpdate()”. As i press “Fire2” character moves but its rotation in z axis starts to increase and stops at some point around “7” which means it completely ignores my rotation command. This gets fixed when i put the rotation command to method “update()”, which makes zero sense to me. I would be glad if someone explains why this happens.(Rest of the script may be irrelevant but i wanted to put it just in case.)
public Sprite[] idleAnimation;
public Sprite[] jumpingAnimation;
public Sprite[] runAnimation;
public float jumpingVelocity;
public float runningVelocity;
SpriteRenderer mySprite;
Rigidbody2D physics;
int idleAnimNumber=0;
int RunningAnimNumber = 0;
bool idleAnimationControl=true;
bool runningAnimationControl = true;
void Start () {
mySprite = GetComponent<SpriteRenderer>();
physics = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
transform.eulerAngles = new Vector3(0,transform.rotation.y , 0);
physics.velocity = new Vector3(0, physics.velocity.y, 0);
if (Input.GetButton("Fire2"))
{
physics.velocity = new Vector3(runningVelocity, physics.velocity.y, 0);
}
if (idleAnimationControl)
{
mySprite.sprite = idleAnimation[idleAnimNumber];
idleAnimNumber++;
if (idleAnimNumber == 11)
{
idleAnimationControl = false;
}
}
else
{
mySprite.sprite = idleAnimation[idleAnimNumber];
idleAnimNumber--;
if (idleAnimNumber == 0)
{
idleAnimationControl = true;
}
}
if (physics.velocity.x != 0)
{
if (runningAnimationControl)
{
mySprite.sprite = runAnimation[idleAnimNumber];
RunningAnimNumber++;
if (RunningAnimNumber == 17)
{
runningAnimationControl = false;
}
}
else
{
mySprite.sprite = runAnimation[idleAnimNumber];
RunningAnimNumber--;
if (RunningAnimNumber == 0)
{
runningAnimationControl = true;
}
}
}
if (Input.GetButton("Fire1"))
{
if (physics.velocity.y == 0)
{
physics.velocity = new Vector3(physics.velocity.x, jumpingVelocity, 0);
}
}
if (physics.velocity.y > 0)
{
mySprite.sprite = jumpingAnimation[0];
}
else if (physics.velocity.y < 0)
{
mySprite.sprite = jumpingAnimation[1];
}
}