Advertisement
Redxone

[UNITY] - Basic Platformer Code

Aug 17th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerControl: MonoBehaviour {
  5.  
  6.     public float mSpd = 50f;
  7.     public float maxSpd = 8f;
  8.     public float jmpSpd = 400f;
  9.     bool isWalking;
  10.     public bool onWall=false;
  11.     int wallJumps = 0;
  12.     float defaultGravity;
  13.     int maxWJmps = 3;
  14.     bool canDoubleJmp;
  15.     Vector2 jumpFace;
  16.  
  17.  
  18.     public bool onGround=false;
  19.     Animator anim;
  20.  
  21.     private Rigidbody2D body;
  22.  
  23.     void Start () {
  24.         anim = GetComponent<Animator> ();
  25.         body = gameObject.GetComponent<Rigidbody2D> ();
  26.         defaultGravity = body.gravityScale;
  27.     }
  28.    
  29.     // Update is called once per frame
  30.     void Update () {
  31.  
  32.         float hspd = Input.GetAxis("Horizontal");
  33.  
  34.         if (Input.GetAxis("Horizontal") != 0) {
  35.             isWalking=true;
  36.         }else
  37.         {
  38.             isWalking=false;
  39.         }
  40.  
  41.         //Set scale based on walk direction
  42.         if (Input.GetAxis ("Horizontal") < 0) {
  43.                 jumpFace = Vector2.right;
  44.                 transform.localScale = new Vector3 (-1, 1, 1);
  45.         }
  46.  
  47.         if (Input.GetAxis ("Horizontal") > 0) {
  48.                 jumpFace = Vector2.left;
  49.                 transform.localScale = new Vector3 (1, 1, 1);
  50.         }  
  51.  
  52.         // Movement
  53.         body.AddForce (Vector2.right * mSpd * hspd);
  54.  
  55.  
  56.         if (onGround && wallJumps > 0)
  57.             wallJumps = 0;
  58.  
  59.         if (onWall && !onGround && wallJumps < maxWJmps) {
  60.             body.gravityScale = 0;
  61.             body.velocity = new Vector2(body.velocity.x, 0);
  62.  
  63.         } else if (!onWall) {
  64.             body.gravityScale = defaultGravity;
  65.         }
  66.  
  67.         if (Input.GetButtonDown ("Jump")) {
  68.             if (onGround) {
  69.                 body.AddForce (Vector2.up * jmpSpd);
  70.                 canDoubleJmp = true;
  71.             }else if (canDoubleJmp) {
  72.                 canDoubleJmp = false;
  73.                 anim.Play("Player_DoubleJump");
  74.                 body.velocity = new Vector2 (body.velocity.x, 0);
  75.                 body.AddForce (Vector2.up * jmpSpd );
  76.             }
  77.  
  78.             if(onWall && wallJumps < maxWJmps)
  79.             {
  80.                 body.velocity = new Vector2 (body.velocity.x, 0);
  81.                 body.AddForce (Vector2.up * jmpSpd);
  82.                 body.velocity = new Vector2 (0, body.velocity.y);
  83.                 body.AddForce (jumpFace * jmpSpd);
  84.                 wallJumps++;
  85.                 canDoubleJmp = true;
  86.             }
  87.  
  88.  
  89.         }
  90.  
  91.         anim.SetBool ("onGround", onGround);
  92.         anim.SetBool ("Walking",isWalking);
  93.         anim.SetBool ("onWall", onWall);
  94.     }
  95.  
  96.     void FixedUpdate()
  97.     {
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.         if (body.velocity.x > maxSpd) {
  105.             body.velocity = new Vector2 (maxSpd, body.velocity.y);
  106.         }
  107.         if (body.velocity.x < -maxSpd) {
  108.             body.velocity = new Vector2 (-maxSpd, body.velocity.y);
  109.         }
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement