CakeMeister

2d Shooter player phan 3

Jul 24th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 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, armright = true;
  9.  
  10.     public Rigidbody2D r2;
  11.     public Animator anim;
  12.     // Use this for initialization
  13.     void Start () {
  14.         r2 = gameObject.GetComponent<Rigidbody2D>();
  15.         anim = gameObject.GetComponent<Animator>();
  16.  
  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.  
  35.     private void FixedUpdate()
  36.     {
  37.         float h = Input.GetAxis("Horizontal");
  38.         r2.AddForce((Vector2.right) * speed * h);
  39.  
  40.         if (r2.velocity.x > maxspeed)
  41.             r2.velocity = new Vector2(maxspeed, r2.velocity.y);
  42.  
  43.         if (r2.velocity.x < -maxspeed)
  44.             r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
  45.  
  46.         if (armright && !faceright)
  47.             flip();
  48.         if (armright == false && faceright)
  49.             flip();
  50.  
  51.         if (grounded)
  52.         {
  53.             r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
  54.         }
  55.     }
  56.  
  57.     public void flip()
  58.     {
  59.         faceright = !faceright;
  60.         Vector3 scale;
  61.         scale = transform.localScale;
  62.         scale.x *= -1;
  63.         transform.localScale = scale;
  64.     }
  65. }
Add Comment
Please, Sign In to add comment