Advertisement
CakeMeister

Player scripts phan 3

Jun 22nd, 2017
2,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 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, doublejump = false;
  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.                 doublejump = true;
  30.                 r2.AddForce(Vector2.up*jumpPow);
  31.  
  32.             }
  33.             else
  34.             {
  35.                 if (doublejump)
  36.                 {
  37.                     doublejump = false;
  38.                     r2.velocity = new Vector2(r2.velocity.x, 0);
  39.                     r2.AddForce(Vector2.up * jumpPow * 0.7f);
  40.                 }
  41.             }
  42.  
  43.         }
  44.     }
  45.  
  46.     void FixedUpdate()
  47.     {
  48.         float h = Input.GetAxis("Horizontal");
  49.         r2.AddForce((Vector2.right) * speed * h);
  50.  
  51.         if (r2.velocity.x > maxspeed)
  52.             r2.velocity = new Vector2(maxspeed, r2.velocity.y);
  53.         if (r2.velocity.x < -maxspeed)
  54.             r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
  55.        
  56.         if (h>0 && !faceright)
  57.         {
  58.             Flip();
  59.         }
  60.  
  61.         if (h < 0 && faceright)
  62.         {
  63.             Flip();
  64.         }
  65.     }
  66.  
  67.     public void Flip()
  68.     {
  69.         faceright = !faceright;
  70.         Vector3 Scale;
  71.         Scale = transform.localScale;
  72.         Scale.x *= -1;
  73.         transform.localScale = Scale;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement