Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

My character keeps flipping with flip script

Discussion in '2D' started by janjicm, Apr 30, 2020.

  1. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    Here is the script:

    It looks like this:


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

    public class Kontrola : MonoBehaviour
    {
    [SerializeField] private LayerMask platformsLayerMask;
    [SerializeField] private Transform pfDoubleJumpEffect;
    [SerializeField] private Transform pfAirJumpIcon;

    public float moveSpeed = 10f;

    private Rigidbody2D rigidbody2d;
    private BoxCollider2D boxCollider2d;
    private int airJumpCount;
    private int airJumpCountMax;
    private List<GameObject> airJumpIconGameObjectList;

    [Header("Wall Jump")]
    public float wallJumpTime = 0.2f;
    public float wallSlideSPeed = 0.3f;
    public float wallDistance = 0.5f;
    bool isWallSliding = false;
    RaycastHit2D WallCheckHit;
    float jumpTime;

    bool isFacingRight = true;

    private void Awake()
    {
    rigidbody2d = transform.GetComponent<Rigidbody2D>();
    boxCollider2d = transform.GetComponent<BoxCollider2D>();
    SetAirJumpCountMax(1);
    }

    private void Update(){
    if (IsGrounded())
    {
    ResetAirJumpCount();
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
    if(IsGrounded())
    {
    float jumpVelocity = 12f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    }
    else if(isWallSliding && Input.GetKey(KeyCode.UpArrow))
    {
    float jumpVelocity = 12f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    }
    else
    {
    if(Input.GetKeyDown(KeyCode.UpArrow ))
    {
    if(airJumpCount < airJumpCountMax)
    {
    Instantiate(pfDoubleJumpEffect, transform.position, Quaternion.identity);
    float jumpVelocity = 12f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    SpendAirJump();
    }
    }
    }
    }

    HandleMovement();
    }

    private void SetAirJumpCountMax(int airJumpCountMax)
    {
    this.airJumpCountMax = airJumpCountMax;
    airJumpIconGameObjectList = new List<GameObject>();
    for (int i = 0; i < airJumpCountMax; i++)
    {
    Transform airJumpIconTransform = Instantiate(pfAirJumpIcon, transform);
    airJumpIconTransform.localPosition = new Vector3(-0.15f, 1f * i, 0.0009f);
    airJumpIconGameObjectList.Add(airJumpIconTransform.gameObject);
    }
    }

    private void SpendAirJump(){
    airJumpCount++;
    for (int i = 0; i < airJumpCount; i++)
    {
    airJumpIconGameObjectList.SetActive(false);
    }
    }

    private void ResetAirJumpCount()
    {
    if(airJumpCount > 0){
    airJumpCount = 0;
    for (int i = 0; i < airJumpIconGameObjectList.Count; i++)
    {
    airJumpIconGameObjectList.SetActive(true);
    }
    }
    }

    private bool IsGrounded()
    {
    RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .4f, platformsLayerMask);
    return raycastHit2d.collider != null;
    }

    void HandleMovement()
    {
    float moveSpeed = 7f;
    if (Input.GetKey(KeyCode.LeftArrow))
    {
    rigidbody2d.velocity = new Vector2(-moveSpeed, rigidbody2d.velocity.y);
    isFacingRight = false;

    Flip();

    }
    else
    {
    if (Input.GetKey(KeyCode.RightArrow))
    {
    rigidbody2d.velocity = new Vector2(+moveSpeed, rigidbody2d.velocity.y);
    isFacingRight = true;

    Flip();
    }
    else
    {
    rigidbody2d.velocity = new Vector2(0, rigidbody2d.velocity.y);
    }
    }

    }

    void FixedUpdate(){

    if (isFacingRight)
    {
    WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(wallDistance, 0), wallDistance, platformsLayerMask);
    }
    else
    {
    WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(-wallDistance, 0), wallDistance, platformsLayerMask);
    }

    if (WallCheckHit && moveSpeed != 0)
    {
    isWallSliding = true;
    jumpTime = Time.time + wallJumpTime;
    }
    else if (jumpTime < Time.time)
    {
    isWallSliding = false;
    }

    if (isWallSliding)
    {
    rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, Mathf.Clamp(rigidbody2d.velocity.y, wallSlideSPeed, float.MaxValue));
    }
    }

    void Flip ()
    {
    transform.Rotate(0, 180, 0);
    }

    }
     
  2. nabrown

    nabrown

    Joined:
    Jun 27, 2019
    Posts:
    27
    I'm not gonna read all of that lol. Have you tried using the flip built into the sprite renderer instead of transform.rotate? or use scale.x = -1? Also, where is your sprites rotational axis setup?