Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour {
- public float speed = 8f;
- private float normSpeed;
- public float jumpForce = 12f;
- public float bounceForce = 20f;
- private bool isBouncing = false;
- public float gravity = 40f;
- public float iceSpeed = 14f;
- private Vector3 moveDirection = Vector3.zero;
- private Vector3 bounceVector = Vector3.zero;
- private Vector3 iceVector = Vector3.zero;
- public int maxJumpAmount;
- private int numberOfJumps = 0;
- public float collapsedSize = 0.5f;
- public float normalizedSize = 1f;
- public float expandedSize = 1.5f;
- private void Start()
- {
- normSpeed = speed;
- }
- private void Update ()
- {
- CharacterController controller = gameObject.GetComponent<CharacterController>();
- if (controller.isGrounded)
- {
- numberOfJumps = 0; // Reset the amount of jumps the player has used since the player is grounded
- if (isBouncing)
- {
- moveDirection = bounceVector;
- bounceVector = Vector3.zero;
- isBouncing = false;
- } else
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- moveDirection = Vector3.ClampMagnitude(moveDirection, 1);
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- }
- } else
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection.x *= speed / 2;
- moveDirection.z *= speed / 2;
- }
- if (Input.GetButtonDown("Jump") && numberOfJumps < maxJumpAmount)
- {
- moveDirection.y = jumpForce;
- // Increase the amount of jumps so you can reach the max amount of jumps.
- numberOfJumps++;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- }
- private void OnControllerColliderHit(ControllerColliderHit hit)
- {
- if (hit.collider.tag == "Obstacle" || hit.collider.tag == "Ground")
- {
- speed = normSpeed;
- isBouncing = false;
- }
- else if (hit.collider.tag == "IceObstacle")
- {
- speed = iceSpeed;
- }
- else if (hit.collider.tag == "BounceObstacle")
- {
- bounceVector = new Vector3(0, bounceForce, 0);
- bounceVector = Vector3.ClampMagnitude(bounceVector, 1);
- bounceVector = transform.TransformDirection(bounceVector);
- bounceVector *= bounceForce;
- isBouncing = true;
- }
- if (hit.collider.tag == "ResetObstacle")
- {
- transform.localScale = new Vector3(normalizedSize, normalizedSize, normalizedSize);
- }
- else if (hit.collider.tag == "ExpandObstacle")
- {
- transform.localScale = new Vector3(expandedSize, expandedSize, expandedSize);
- }
- else if (hit.collider.tag == "CollapseObstacle")
- {
- transform.localScale = new Vector3(collapsedSize, collapsedSize, collapsedSize);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement