Advertisement
CakeMeister

Player scripts phan 4

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