Advertisement
Guest User

MoveAndFlip.cs

a guest
Apr 5th, 2020
2,731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MoveAndFlip : MonoBehaviour
  6. {
  7.  
  8.     [Header("Is your character facing right before runtime? If yes, check box")]
  9.     public bool facingRight;
  10.  
  11.     public float horizontalValue;
  12.     public float speed;
  13.  
  14.     // Update is called once per frame
  15.     void Update()
  16.     {
  17.         move();
  18.  
  19.         // use this for the wrong way
  20.         //wrongWayToFlip();
  21.  
  22.         // use this for a better way
  23.         properFlip();
  24.     }
  25.  
  26.     void move()
  27.     {
  28.         horizontalValue = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
  29.         //transform.Translate(new Vector3()
  30.     }
  31.  
  32.     void properFlip()
  33.     {
  34.         if((horizontalValue < 0 && facingRight) || (horizontalValue > 0 && !facingRight))
  35.         {
  36.             facingRight = !facingRight;
  37.             transform.Rotate(new Vector3(0, 180, 0));
  38.         }
  39.     }
  40.  
  41.     void wrongWayToFlip()
  42.     {
  43.         if ((horizontalValue < 0 && facingRight) || (horizontalValue > 0 && !facingRight))
  44.         {
  45.             facingRight = !facingRight;
  46.             Vector3 theScale = transform.localScale;
  47.             theScale.x *= -1;
  48.             transform.localScale = theScale;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement