Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(Controller2D))]
- public class Player : MonoBehaviour {
- public float maxJumpHeight = 4;
- public float minJumpHeight = 1;
- public float timeToJumpApex = .4f;
- float accelerationTimeAirborne = .2f;
- float accelerationTimeGrounded = .1f;
- static float origMoveSpeed = 6;
- float moveSpeed = origMoveSpeed;
- public Vector2 wallJumpClimb;
- public Vector2 wallJumpOff;
- public Vector2 wallLeap;
- public float wallSlideSpeedMax = 3;
- public float wallStickTime = .25f;
- float timeToWallUnstick;
- float gravity;
- float maxJumpVelocity;
- float minJumpVelocity;
- Vector3 velocity;
- float velocityXSmoothing;
- float waitUntil;
- private bool doubleJump;
- /*
- private bool dashSpeed;
- private bool keyPressedTwice;*/
- public float buttonCoolerOrig = .8f;
- float buttonCooler;
- int buttonCount = 0;
- [Range (0,5)] //so u dont set it to something dumb like 9001
- public float speedMultiplier = 2.0f;
- Controller2D controller;
- void Start() {
- waitUntil = Time.time;
- buttonCooler = buttonCoolerOrig;
- controller = GetComponent<Controller2D>();
- gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
- maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
- minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
- print("Gravity:" + gravity + "Jump Velocity:" + maxJumpVelocity);
- }
- void FixedUpdate() {
- //float waitUntil = Time.time;
- //print("moveSpeed is :" + moveSpeed);
- //Debug.Log(moveSpeed);
- if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow)) {
- if (buttonCooler > 0 && buttonCount == 1 && waitUntil <= Time.time/*Number of Taps you want Minus One*/) {
- //Has double tapped
- //Debug.Log("Double Tapped");
- moveSpeed *= speedMultiplier;
- //buttonCooler -= 2 * Time.deltaTime;
- Debug.Log("DoubleTapped: " + Time.time);
- waitUntil = Time.time + 0.2f;
- print(waitUntil);
- } else {
- buttonCooler = buttonCoolerOrig;
- buttonCount += 1;
- }
- }
- if (buttonCooler > 0) {
- buttonCooler -= 1 * Time.deltaTime;
- } else {
- buttonCount = 0;
- //Debug.Log("Reset");
- }
- checkMoveSpeed();
- }
- private void checkMoveSpeed() {
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- if (input.x == 0) {
- moveSpeed = origMoveSpeed;
- //Debug.Log("Input 0'd");
- }
- if (moveSpeed > origMoveSpeed * speedMultiplier) {
- moveSpeed = origMoveSpeed * speedMultiplier;
- }
- }
- void Update() {
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- int wallDirX = (controller.collisions.left) ? -1 : 1;
- float targetVelocityX = input.x * moveSpeed;
- velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
- //print(velocity.x);
- bool wallSliding = false;
- if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) {
- wallSliding = true;
- if (velocity.y < -wallSlideSpeedMax) {
- velocity.y = -wallSlideSpeedMax;
- }
- if (timeToWallUnstick > 0) {
- velocityXSmoothing = 0;
- velocity.x = 0;
- if (input.x != wallDirX && input.x != 0) {
- timeToWallUnstick -= Time.deltaTime;
- }
- else {
- timeToWallUnstick = wallStickTime;
- }
- }
- else {
- timeToWallUnstick = wallStickTime;
- }
- }
- if (Input.GetKeyDown(KeyCode.Space)) {
- if (wallSliding) {
- doubleJump = true;
- if (wallDirX == input.x) {
- velocity.x = -wallDirX * wallJumpClimb.x;
- velocity.y = wallJumpClimb.y;
- }
- else if (input.x == 0) {
- velocity.x = -wallDirX * wallJumpOff.x;
- velocity.y = wallJumpOff.y;
- }
- else {
- velocity.x = -wallDirX * wallLeap.x;
- velocity.y = wallLeap.y;
- }
- }
- if (controller.collisions.below) {
- velocity.y = maxJumpVelocity;
- }
- }
- if (Input.GetKeyUp(KeyCode.Space))
- if (velocity.y > minJumpVelocity) {
- velocity.y = minJumpVelocity;
- }
- if (controller.collisions.below || wallSliding) {
- doubleJump = true;
- }
- if (!controller.collisions.below && doubleJump == true) {
- if (Input.GetKeyDown(KeyCode.Space)) {
- velocity.y = maxJumpVelocity;
- doubleJump = false;
- }
- }
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime, input);
- if (controller.collisions.above || controller.collisions.below) {
- velocity.y = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement