C
C#2y ago
Kuroko

❔ Please Help! When I flip my character he moves backwards

Hey guys... so I have a problem with my player controller... when I flip it to the other direction my character teleporting backwards... please if anyone knows what's the problem help! 🙂
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
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 LayerMask whatIsGround;

private int extraJump;
public int extraJumpValue;

// Start is called before the first frame update
void Start()
{
extraJump = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}

void Update()
{
if (isGrounded == true)
{
extraJump = extraJumpValue;
}

if (Input.GetKeyDown(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}

void Flip()
{
facingRight = !facingRight;
transform.Rotate(0, 180, 0);

}
}
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
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 LayerMask whatIsGround;

private int extraJump;
public int extraJumpValue;

// Start is called before the first frame update
void Start()
{
extraJump = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}

void Update()
{
if (isGrounded == true)
{
extraJump = extraJumpValue;
}

if (Input.GetKeyDown(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}

void Flip()
{
facingRight = !facingRight;
transform.Rotate(0, 180, 0);

}
}
5 Replies
MasterSubarashii
$unity
MasterSubarashii
they probably know better
Kuroko
KurokoOP2y ago
Thank you
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server