Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerControl: MonoBehaviour {
- public float mSpd = 50f;
- public float maxSpd = 8f;
- public float jmpSpd = 400f;
- bool isWalking;
- public bool onWall=false;
- int wallJumps = 0;
- float defaultGravity;
- int maxWJmps = 3;
- bool canDoubleJmp;
- Vector2 jumpFace;
- public bool onGround=false;
- Animator anim;
- private Rigidbody2D body;
- void Start () {
- anim = GetComponent<Animator> ();
- body = gameObject.GetComponent<Rigidbody2D> ();
- defaultGravity = body.gravityScale;
- }
- // Update is called once per frame
- void Update () {
- float hspd = Input.GetAxis("Horizontal");
- if (Input.GetAxis("Horizontal") != 0) {
- isWalking=true;
- }else
- {
- isWalking=false;
- }
- //Set scale based on walk direction
- if (Input.GetAxis ("Horizontal") < 0) {
- jumpFace = Vector2.right;
- transform.localScale = new Vector3 (-1, 1, 1);
- }
- if (Input.GetAxis ("Horizontal") > 0) {
- jumpFace = Vector2.left;
- transform.localScale = new Vector3 (1, 1, 1);
- }
- // Movement
- body.AddForce (Vector2.right * mSpd * hspd);
- if (onGround && wallJumps > 0)
- wallJumps = 0;
- if (onWall && !onGround && wallJumps < maxWJmps) {
- body.gravityScale = 0;
- body.velocity = new Vector2(body.velocity.x, 0);
- } else if (!onWall) {
- body.gravityScale = defaultGravity;
- }
- if (Input.GetButtonDown ("Jump")) {
- if (onGround) {
- body.AddForce (Vector2.up * jmpSpd);
- canDoubleJmp = true;
- }else if (canDoubleJmp) {
- canDoubleJmp = false;
- anim.Play("Player_DoubleJump");
- body.velocity = new Vector2 (body.velocity.x, 0);
- body.AddForce (Vector2.up * jmpSpd );
- }
- if(onWall && wallJumps < maxWJmps)
- {
- body.velocity = new Vector2 (body.velocity.x, 0);
- body.AddForce (Vector2.up * jmpSpd);
- body.velocity = new Vector2 (0, body.velocity.y);
- body.AddForce (jumpFace * jmpSpd);
- wallJumps++;
- canDoubleJmp = true;
- }
- }
- anim.SetBool ("onGround", onGround);
- anim.SetBool ("Walking",isWalking);
- anim.SetBool ("onWall", onWall);
- }
- void FixedUpdate()
- {
- if (body.velocity.x > maxSpd) {
- body.velocity = new Vector2 (maxSpd, body.velocity.y);
- }
- if (body.velocity.x < -maxSpd) {
- body.velocity = new Vector2 (-maxSpd, body.velocity.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement