Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ng using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControls : MonoBehaviour
- {
- public float speed = 3f;
- public float forwardSpeed = 5f;
- public Vector3 velocity;
- public float horizontalVelocityCap = 5f;
- public float verticalVelocityCap = 6f;
- public float gravity = 5f;
- public float wallRunningGravity = 2f;
- public float jumpStrength = 2f;
- public HitBox leftWall;
- public HitBox rightWall;
- public HitBox floor;
- enum Mode { Floor, LeftWall, RightWall }
- private Mode mode = Mode.Floor;
- enum Side { Left, Right }
- private Side side = Side.Left;
- void Update()
- {
- switch(mode)
- {
- case Mode.Floor:
- HandleFloorControls();
- break;
- case Mode.LeftWall:
- case Mode.RightWall:
- HandleWallRunningControls();
- break;
- }
- if (floor.colliding && velocity.y < 0)
- velocity.y = 0f;
- else if (velocity.y < -verticalVelocityCap)
- velocity.y = -verticalVelocityCap;
- if (velocity.x > horizontalVelocityCap)
- velocity.x = horizontalVelocityCap;
- else if (velocity.x < -horizontalVelocityCap)
- velocity.x = -horizontalVelocityCap;
- velocity.z = forwardSpeed;
- transform.position += velocity * Time.deltaTime;
- }
- private void HandleFloorControls()
- {
- if (!leftWall.colliding && Input.GetKey(KeyCode.LeftArrow))
- {
- velocity += Vector3.left * speed;
- }
- if (!rightWall.colliding && Input.GetKey(KeyCode.RightArrow))
- {
- velocity = Vector3.right * speed;
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- if(leftWall.colliding)
- mode = Mode.LeftWall;
- else if (rightWall.colliding)
- mode = Mode.RightWall;
- else
- {
- velocity += jumpStrength * Vector3.up;
- }
- }
- velocity += gravity * Vector3.down;
- }
- private void HandleWallRunningControls()
- {
- velocity += wallRunningGravity * Vector3.down;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement