- Home /
Having trouble with flipping in my script. ,flipping my character doesn't work
For some reason when i test my script right away Facing right is false when i set it to true and nothing happens can someone help me I want my script to flip with the scale. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private LayerMask platformLayerMask;
float speed = 10f;
float jumpForce = 15f;
float distanceToCheck = 0.1f;
public BoxCollider2D Box2D;
float gravity = -9.81f;
float velocity;
float gravityScale = 2f;
float horizantalInput = 0f;
[SerializeField] public bool FacingRight = true;
private void Start()
{
GetComponent<SpriteRenderer>();
GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
//gravity
velocity += gravity * gravityScale * Time.deltaTime;
//stop object from going through the floor by setting velocity to 0
if (isGrounded() && velocity < 0)
{
velocity = 0;
}
//movment jump
if (isGrounded() && (Input.GetKey(KeyCode.Space)))
{
velocity = jumpForce;
}
transform.Translate(new Vector3(0, velocity, 0) * Time.deltaTime);
//movement horizontally
horizantalInput = Input.GetAxis("Horizontal");
if (horizantalInput != 0f)
{
transform.Translate(Vector3.right * speed * Time.deltaTime * horizantalInput);
}
//Flip issue
if (FacingRight = false && horizantalInput > 0)
{
FlipRight();
}
if (FacingRight = true && horizantalInput < 0)
{
FlipLeft();
}
}
void FlipRight()
{
FacingRight = true;
transform.Rotate(0, 180, 0);
}
void FlipLeft()
{
FacingRight = false;
transform.Rotate(0, 0, 0);
}
,For some reason my script doesn't work it is spoud to start off with faceing right = True but as soom as i test it right away face right = false and when i move around nothing happens i need help if (horizantalInput != 0f) { transform.Translate(Vector3.right * speed * Time.deltaTime * horizantalInput); } if (FacingRight = false && horizantalInput > 0) { FlipRight(); } if (FacingRight = true && horizantalInput < 0) { FlipLeft(); } } void FlipRight() { FacingRight = true; transform.Rotate(0, 180, 0); } void FlipLeft() { FacingRight = false; transform.Rotate(0, 0, 0); }