Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class ControllerScript : MonoBehaviour
- {
- public float maxSpeed = 10f;
- public bool facingRight = true;
- Animator anim;
- bool grounded = false;
- public bool touchingWall = false;
- public Transform groundCheck;
- public Transform wallCheck;
- float groundRadius = 0.2f;
- float wallTouchRadius = 0.2f;
- public LayerMask whatIsGround;
- public LayerMask whatIsWall;
- public float jumpForce = 700f;
- public float jumpPushForce = 10f;
- bool doubleJump = false;
- public bool hasJumped = false;
- public bool hasWallJumped = false;
- public float move = 100f;
- void Start ()
- {
- anim = GetComponent<Animator> ();
- }
- void FixedUpdate ()
- {
- grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
- touchingWall = Physics2D.OverlapCircle (wallCheck.position, wallTouchRadius, whatIsWall);
- anim.SetBool ("touchingWall", touchingWall);
- anim.SetBool ("Ground", grounded);
- if (grounded) {
- doubleJump = false;
- hasWallJumped = false;
- touchingWall = false;
- }
- anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
- float move = Input.GetAxis ("Horizontal");
- anim.SetFloat ("Speed", Mathf.Abs (move));
- rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
- if (move > 0 && !facingRight)
- Flip ();
- else if (move < 0 && facingRight)
- Flip ();
- if ((!grounded && touchingWall) && Input.GetButtonDown ("Jump")) {
- rigidbody2D.velocity = Vector2.zero;
- rigidbody2D.velocity = new Vector2 (maxSpeed * 10, rigidbody2D.velocity.y);
- }
- }
- void Update ()
- {
- if ((grounded || !doubleJump) && Input.GetButtonDown ("Jump")) {
- anim.SetBool ("Ground", false);
- rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, 10.5f);
- if (!doubleJump && !grounded) {
- doubleJump = true;
- }
- if ((!grounded && touchingWall) && Input.GetButtonDown ("Jump")) {
- hasWallJumped = true;
- Debug.LogWarning("haswalljumped");
- }
- }
- }
- void Flip ()
- {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement