Hi,
I added animation and fliping into my game but then it suddenly extra flips. Please help
here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public bool hitEdge;
public float speed;
public float jumpForce;
private float moveInput;
private bool facingRight = true;
private Rigidbody2D rb;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
private int nextSceneToLoad;
private bool hi;
public Animator animator;
private GameMaster gm;
GameObject Player;
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Start()
{
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
transform.position = gm.lastCheckPointPos;
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
nextSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
}
void FixedUpdate()
{
if (facingRight == false && moveInput > 0)
{
Flip();
}else if (facingRight = true && moveInput < 0)
{
Flip();
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
private void Update()
{
animator.SetFloat("Speed", Mathf.Abs(moveInput));
if (isGrounded == true)
{
animator.SetBool("IsJumping", false);
extraJumps = extraJumpsValue;
}
else if (isGrounded == false)
{
animator.SetBool("IsJumping", true);
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Danger")
{
Scene scene;
scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
FindObjectOfType<AudioManager>().Play("Death");
}
if (collision.collider.tag == "Door")
{
hi = true;
SceneManager.LoadScene(nextSceneToLoad);
}
}
}
Thank you for advance.