Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public GameObject cameraBase;
- public float movementSpeed;
- public float jumpVelocity;
- public float fallMultiplier = 2.5f;
- public float lowJumpMultiplier = 2f;
- public float distanceToGround;
- public int numberOfJumps;
- public int jumpsLeft;
- Rigidbody playerBody;
- Collider playerCollider;
- public Vector3 currentVelocity;
- float inputX;
- float inputZ;
- Vector3 movement;
- // Start is called before the first frame update
- void Start()
- {
- jumpsLeft = numberOfJumps;
- playerBody = GetComponent<Rigidbody>();
- playerCollider = GetComponent<CapsuleCollider>();
- distanceToGround = playerCollider.bounds.extents.y;
- }
- // Update is called once per frame
- void Update()
- {
- if (isGrounded()) jumpsLeft = numberOfJumps;
- if (playerBody.velocity.y < 0)
- {
- playerBody.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
- }
- else if (playerBody.velocity.y > 0 && !Input.GetKey(KeyCode.Space) || !Input.GetButton("Jump") || jumpsLeft < numberOfJumps - 1)
- {
- playerBody.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
- }
- }
- private void FixedUpdate()
- {
- inputX = Input.GetAxis("Horizontal");
- inputZ = Input.GetAxis("Vertical");
- if (Input.GetKeyDown(KeyCode.Space) && jumpsLeft > 0)
- {
- jumpsLeft--;
- playerBody.velocity = Vector3.up * jumpVelocity;
- }
- movement = new Vector3(inputX * movementSpeed, playerBody.velocity.y, inputZ * movementSpeed);
- Vector3 actualMotion = Quaternion.Euler(0, cameraBase.transform.eulerAngles.y, 0) * movement;
- /*
- if(Input.GetKey(KeyCode.W))
- {
- playerBody.MovePosition(transform.position + (cameraBase.transform.forward * movementSpeed * Time.deltaTime));
- }
- if (Input.GetKey(KeyCode.S))
- {
- playerBody.MovePosition(transform.position + (-cameraBase.transform.forward * movementSpeed * Time.deltaTime));
- }
- if (Input.GetKey(KeyCode.D))
- {
- playerBody.MovePosition(transform.position + (cameraBase.transform.right * movementSpeed * Time.deltaTime));
- }
- if (Input.GetKey(KeyCode.A))
- {
- playerBody.MovePosition(transform.position + (-cameraBase.transform.right * movementSpeed * Time.deltaTime));
- }
- */
- playerBody.velocity = actualMotion;
- }
- private bool isGrounded()
- {
- return Physics.Raycast(transform.position, -Vector3.up, distanceToGround + 0.01f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement