Advertisement
Guest User

Untitled

a guest
May 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8.     public GameObject cameraBase;
  9.     public float movementSpeed;
  10.     public float jumpVelocity;
  11.     public float fallMultiplier = 2.5f;
  12.     public float lowJumpMultiplier = 2f;
  13.     public float distanceToGround;
  14.     public int numberOfJumps;
  15.     public int jumpsLeft;
  16.     Rigidbody playerBody;
  17.     Collider playerCollider;
  18.  
  19.     public Vector3 currentVelocity;
  20.  
  21.     float inputX;
  22.     float inputZ;
  23.     Vector3 movement;
  24.  
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.         jumpsLeft = numberOfJumps;
  29.         playerBody = GetComponent<Rigidbody>();
  30.         playerCollider = GetComponent<CapsuleCollider>();
  31.         distanceToGround = playerCollider.bounds.extents.y;
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.         if (isGrounded()) jumpsLeft = numberOfJumps;
  38.  
  39.         if (playerBody.velocity.y < 0)
  40.         {
  41.             playerBody.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
  42.         }
  43.         else if (playerBody.velocity.y > 0 && !Input.GetKey(KeyCode.Space) || !Input.GetButton("Jump") || jumpsLeft < numberOfJumps - 1)
  44.         {
  45.             playerBody.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
  46.         }
  47.     }
  48.  
  49.     private void FixedUpdate()
  50.     {
  51.        
  52.         inputX = Input.GetAxis("Horizontal");
  53.         inputZ = Input.GetAxis("Vertical");
  54.  
  55.         if (Input.GetKeyDown(KeyCode.Space) && jumpsLeft > 0)
  56.         {
  57.             jumpsLeft--;
  58.             playerBody.velocity = Vector3.up * jumpVelocity;
  59.         }
  60.  
  61.         movement = new Vector3(inputX * movementSpeed, playerBody.velocity.y, inputZ * movementSpeed);
  62.  
  63.         Vector3 actualMotion = Quaternion.Euler(0, cameraBase.transform.eulerAngles.y, 0) * movement;
  64.  
  65.         /*
  66.         if(Input.GetKey(KeyCode.W))
  67.         {
  68.             playerBody.MovePosition(transform.position + (cameraBase.transform.forward * movementSpeed * Time.deltaTime));
  69.         }
  70.         if (Input.GetKey(KeyCode.S))
  71.         {
  72.             playerBody.MovePosition(transform.position + (-cameraBase.transform.forward * movementSpeed * Time.deltaTime));
  73.         }
  74.  
  75.         if (Input.GetKey(KeyCode.D))
  76.         {
  77.             playerBody.MovePosition(transform.position + (cameraBase.transform.right * movementSpeed * Time.deltaTime));
  78.         }
  79.         if (Input.GetKey(KeyCode.A))
  80.         {
  81.             playerBody.MovePosition(transform.position + (-cameraBase.transform.right * movementSpeed * Time.deltaTime));
  82.         }
  83.         */
  84.  
  85.         playerBody.velocity = actualMotion;
  86.        
  87.     }
  88.  
  89.     private bool isGrounded()
  90.     {
  91.         return Physics.Raycast(transform.position, -Vector3.up, distanceToGround + 0.01f);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement