Advertisement
Guest User

Untitled

a guest
Jul 6th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     Rigidbody2D rb;
  8.     Animator anim;
  9.    
  10.  
  11.     void Start()
  12.     {
  13.         rb = GetComponent<Rigidbody2D>();
  14.         anim = GetComponent<Animator>();
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         rb.velocity = new Vector2(Input.GetAxis("Horizontal") * 12f, rb.velocity.y);
  20.  
  21.         if (Input.GetKeyDown(KeyCode.Space))
  22.         {
  23.             PlayerJumping();
  24.             anim.SetTrigger("IsJump");
  25.            
  26.         }
  27.  
  28.         if(Input.GetAxis("Horizontal") == 0)
  29.         {
  30.             anim.SetBool("IsRuning", true);
  31.         }
  32.         else
  33.         {
  34.             Flip();
  35.             anim.SetBool("IsRuning", false);
  36.         }
  37.     }
  38.  
  39.     void PlayerJumping()
  40.     {
  41.         rb.AddForce(transform.up * 16f, ForceMode2D.Impulse);
  42.     }
  43.  
  44.     void Flip()
  45.     {
  46.         if(Input.GetAxis("Horizontal") < 0)
  47.         {
  48.             transform.localRotation = Quaternion.Euler(0, 180, 0);
  49.         }
  50.  
  51.         if (Input.GetAxis("Horizontal") > 0)
  52.         {
  53.             transform.localRotation = Quaternion.Euler(0, 0, 0);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement