Advertisement
CakeMeister

Player scripts - Phan 2

Jun 22nd, 2017
5,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.  
  7.     public float speed = 50f, maxspeed = 3, jumpPow = 220f;
  8.     public bool grounded = true, faceright = true;
  9.    
  10.     public Rigidbody2D r2;
  11.     public Animator anim;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.         r2 = gameObject.GetComponent<Rigidbody2D>();
  16.         anim = gameObject.GetComponent<Animator>();
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update () {
  21.         anim.SetBool("Grounded", grounded);
  22.         anim.SetFloat("Speed", Mathf.Abs(r2.velocity.x));
  23.  
  24.         if (Input.GetKeyDown(KeyCode.Space))
  25.         {
  26.             if (grounded)
  27.             {
  28.                 grounded = false;
  29.                 r2.AddForce(Vector2.up * jumpPow);
  30.             }
  31.         }
  32.     }
  33.  
  34.     void FixedUpdate()
  35.     {
  36.         float h = Input.GetAxis("Horizontal");
  37.         r2.AddForce((Vector2.right) * speed * h);
  38.  
  39.         if (r2.velocity.x > maxspeed)
  40.             r2.velocity = new Vector2(maxspeed, r2.velocity.y);
  41.         if (r2.velocity.x < -maxspeed)
  42.             r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
  43.        
  44.         if (h>0 && !faceright)
  45.         {
  46.             Flip();
  47.         }
  48.  
  49.         if (h < 0 && faceright)
  50.         {
  51.             Flip();
  52.         }
  53.     }
  54.  
  55.     public void Flip()
  56.     {
  57.         faceright = !faceright;
  58.         Vector3 Scale;
  59.         Scale = transform.localScale;
  60.         Scale.x *= -1;
  61.         transform.localScale = Scale;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement