after adding a script to help flip the character for moving left and right, the idle will flip to face the other direction. How do i stop i prevent it from doing so?
the script i used is this:
using UnityEngine;
using System.Collections;
public class MainCharCtrl : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if(move > 0 &&!facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
long story short how do I stop the idle animation from being affected as well by the script?
Check if it’s idle and save the default idle position in the Start function
using UnityEngine;
using System.Collections;
public class MainCharCtrl : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Transform defaultTransform;
Animator anim;
void Start ()
{
anim = GetComponent ();
defaultTransform = transform;
}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
//================================
if(move !=0)
{
if(move > 0 &&!facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
else
{
transform = defaultTransform;
facingRight=true;//might be needed because it's the default value
}
//========================================================
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Checkmark is on the left for the answer taht solved your problem