Advertisement
Guest User

bUNy script unity

a guest
Jan 28th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public float speed;
  8.     public float jump;
  9.  
  10.     private bool IsGrounded = true;
  11.     private float move;
  12.     private Rigidbody2D rb;
  13.  
  14.     private bool facingRight;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         facingRight = true;
  20.         rb = GetComponent<Rigidbody2D>();
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void FixedUpdate()
  25.     {
  26.         //IsGrounded
  27.         if (IsGrounded == true)
  28.         {
  29.             if (Input.GetButtonDown("Jump"))
  30.             {
  31.                 rb.AddForce(new Vector2(rb.velocity.x, jump));
  32.                 IsGrounded = false;
  33.             }
  34.         }
  35.  
  36.         move = Input.GetAxisRaw("Horizontal");
  37.         rb.velocity = new Vector2(move * speed, rb.velocity.y);
  38.  
  39.         Flip("horizontal");
  40.     }
  41.  
  42.     private void OnCollisionEnter2D(Collision2D collision)
  43.     {
  44.         if (collision.gameObject.tag == "Floor") { IsGrounded = true; }
  45.     }
  46.  
  47.     private void Flip(float horizontal)
  48.     {
  49.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
  50.         {
  51.             facingRight = !facingRight;
  52.  
  53.             Vector3 theScale = transform.localScale;
  54.  
  55.             theScale.x *= -5;
  56.  
  57.             transform.localScale = theScale;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement