daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class controller : MonoBehaviour
  6. {
  7.     public Rigidbody2D rb2d;
  8.     public float playerSpeed;
  9.     public float jumpPower;
  10.     public int directionInput;
  11.     public bool groundCheck;
  12.     public bool facingRight = true;
  13.  
  14.  
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         rb2d = GetComponent<Rigidbody2D>();
  20.     }
  21.  
  22.     // Update is called once per frame
  23.    
  24.         void Update()
  25.         {
  26.  
  27.         if (Input.GetKey(KeyCode.Space))
  28.             Jump();
  29.  
  30.         float moveX = Input.GetAxis("Horizontal");
  31.         if (Input.GetKey(KeyCode.D))
  32.             rb2d.MovePosition(rb2d.position+Vector2.right*moveX*playerSpeed*Time.deltaTime);
  33.         if (Input.GetKey(KeyCode.A))
  34.             rb2d.MovePosition(rb2d.position + Vector2.right * moveX * playerSpeed * Time.deltaTime);
  35.         if ((moveX < 0) && (facingRight))
  36.             {
  37.                 Flip();
  38.             }
  39.  
  40.             if ((moveX > 0) && (!facingRight))
  41.             {
  42.                 Flip();
  43.             }
  44.          
  45.         }
  46.     void OnCollisionEnter2D(Collision2D col)
  47.     {
  48.         if (col.gameObject.tag == "gnd")
  49.         {
  50.             groundCheck = true;
  51.         }
  52.  
  53.     }
  54.         void FixedUpdate()
  55.         {
  56.             rb2d.velocity = new Vector2(playerSpeed * directionInput, rb2d.velocity.y);
  57.         }
  58.  
  59.         public void Move(int InputAxis)
  60.         {
  61.  
  62.             directionInput = InputAxis;
  63.  
  64.         }
  65.  
  66.         public void Jump()
  67.         {
  68.        
  69.             if (groundCheck)
  70.             {
  71.                 rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
  72.             }
  73.         groundCheck = false;
  74.         }
  75.  
  76.         void Flip()
  77.         {
  78.             facingRight = !facingRight;
  79.             Vector3 theScale = transform.localScale;
  80.             theScale.x *= -1;
  81.             transform.localScale = theScale;
  82.         }
  83.  
  84.  
  85.  
  86.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top