Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerMovement : MonoBehaviour {
  4.     public float speed = 8f;
  5.     private float normSpeed;
  6.  
  7.     public float jumpForce = 12f;
  8.     public float bounceForce = 20f;
  9.     private bool isBouncing = false;
  10.  
  11.     public float gravity = 40f;
  12.  
  13.     public float iceSpeed = 14f;
  14.  
  15.     private Vector3 moveDirection = Vector3.zero;
  16.     private Vector3 bounceVector = Vector3.zero;
  17.     private Vector3 iceVector = Vector3.zero;
  18.  
  19.     public int maxJumpAmount;
  20.     private int numberOfJumps = 0;
  21.  
  22.     public float collapsedSize = 0.5f;
  23.     public float normalizedSize = 1f;
  24.     public float expandedSize = 1.5f;
  25.  
  26.     private void Start()
  27.     {
  28.         normSpeed = speed;
  29.     }
  30.  
  31.     private void Update ()
  32.     {
  33.         CharacterController controller = gameObject.GetComponent<CharacterController>();
  34.  
  35.         if (controller.isGrounded)
  36.         {
  37.             numberOfJumps = 0; // Reset the amount of jumps the player has used since the player is grounded
  38.  
  39.             if (isBouncing)
  40.             {
  41.                 moveDirection = bounceVector;
  42.                 bounceVector = Vector3.zero;
  43.                 isBouncing = false;
  44.             } else
  45.             {
  46.                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  47.                 moveDirection = Vector3.ClampMagnitude(moveDirection, 1);
  48.                 moveDirection = transform.TransformDirection(moveDirection);
  49.  
  50.                 moveDirection *= speed;
  51.             }
  52.         } else
  53.         {
  54.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
  55.             moveDirection = transform.TransformDirection(moveDirection);
  56.             moveDirection.x *= speed / 2;
  57.             moveDirection.z *= speed / 2;
  58.         }
  59.  
  60.         if (Input.GetButtonDown("Jump") && numberOfJumps < maxJumpAmount)
  61.         {
  62.             moveDirection.y = jumpForce;
  63.  
  64.             // Increase the amount of jumps so you can reach the max amount of jumps.
  65.             numberOfJumps++;
  66.         }
  67.  
  68.  
  69.  
  70.         moveDirection.y -= gravity * Time.deltaTime;
  71.  
  72.         controller.Move(moveDirection * Time.deltaTime);
  73.     }
  74.  
  75.     private void OnControllerColliderHit(ControllerColliderHit hit)
  76.     {
  77.         if (hit.collider.tag == "Obstacle" || hit.collider.tag == "Ground")
  78.         {
  79.             speed = normSpeed;
  80.             isBouncing = false;
  81.         }
  82.         else if (hit.collider.tag == "IceObstacle")
  83.         {
  84.             speed = iceSpeed;
  85.         }
  86.         else if (hit.collider.tag == "BounceObstacle")
  87.         {
  88.             bounceVector = new Vector3(0, bounceForce, 0);
  89.             bounceVector = Vector3.ClampMagnitude(bounceVector, 1);
  90.             bounceVector = transform.TransformDirection(bounceVector);
  91.  
  92.             bounceVector *= bounceForce;
  93.             isBouncing = true;
  94.         }
  95.  
  96.         if (hit.collider.tag == "ResetObstacle")
  97.         {
  98.             transform.localScale = new Vector3(normalizedSize, normalizedSize, normalizedSize);
  99.         }
  100.         else if (hit.collider.tag == "ExpandObstacle")
  101.         {
  102.             transform.localScale = new Vector3(expandedSize, expandedSize, expandedSize);
  103.         }
  104.         else if (hit.collider.tag == "CollapseObstacle")
  105.         {
  106.             transform.localScale = new Vector3(collapsedSize, collapsedSize, collapsedSize);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement