- Home /
Why is my character not flipping?
I am trying to learn the basics of Unity so I downloaded the latest free version. I created a little place for my character to walk around and a little character (most of which I borrowed from RPG Maker as I am no artist). With that said I created Added a idol and walking animations and hooked them into the animator.
Then I wrote the following:
using UnityEngine;
using System.Collections;
public class character_controller_script : MonoBehaviour {
public float maxSpeed = 10f;
private bool facingRight = true;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
// Move at a speed that is predetermined on the Y axis.
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
anim.SetFloat ("Speed", Mathf.Abs (move));
if (move > 0 && !facingRight) {
Flip();
} else if(move < 0 && !facingRight) {
Flip();
}
}
// Flips the character based on input.
void Flip() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Which is pretty basic. We can all understand whats going on here. The issue is simple: The character will move forwards, the character will move backwards, the animations are all linked and work as expected but the character will not flip. So when I move right, he walks. When I move left, it looks like he is walking backwards....
Did I do something wrong in the flip function?
I was watching this tutorial on character control, thats where I got the code from. When he imported his characters they were all right facing, and as far as I understand the code should just flip them for you.
How do I make it flip?
Answer by tanoshimi · Sep 14, 2014 at 10:20 PM
Your code always makes the character face right because you have a logic error. I suspect line 28 should say:
} else if(move < 0 && facingRight) {
Answer by TheXWolf · Sep 14, 2014 at 11:59 PM
The Flip() method flips the character to face the proper direction. First make sure you begin the scene with the player facing the right way.
Also if move is less than 0 if(move < 0) than the character & the character isn't facing right; !facingRight than it's already pointing the right way. The correct code should be:
if(move < 0 && facingRight)
{
Flip();
}
else if(move > 0 && !facingRight)
{
Flip();
}
Well. $$anonymous$$e too. $$anonymous$$y character isn't flipping. I wrote: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public Layer$$anonymous$$ask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
} else
{
anim.SetBool("isRunning", true );
}
if(moveInput < 0 && facingRight)
{
Flip();
} else if (moveInput > 0 && !facingRight)
{
Flip();
}
}
void Update(){
if(isGrounded == true){
extraJumps = extraJumpsValue;
anim.SetBool("isJumping", false);
}
else
{
anim.SetBool("isJumping", true);
}
if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0)
{
anim.SetTrigger("takeOf");
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
} else if(Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true){
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
anim.SetTrigger("takeOf");
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
but nope it didn't worked
Your answer
Follow this Question
Related Questions
problem with animations running then jumping 3 Answers
change the wrap mode of a 2d animation 0 Answers
2d and character controler 0 Answers
How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers
Animator Condition Not Working 0 Answers