Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- // CharacterController characterController;
- //Something? Useful stuff
- public Transform playerCam;
- public Transform orientation;
- private Rigidbody rb;
- // doubles and floats with unity use floats
- //this is for character movement
- public float maxSpeed = 20;
- public float moveSpeed = 4500;
- public bool grounded; // Boolean or bool is true or false (also 0 and 1)
- public float gravity = 20.0f;
- // for ground stuff...
- public LayerMask whatIsGround;
- public float maxSlopeAngle = 35f;
- //This is controlling camera maths
- public float sensitivity = 50f;
- public float sensMultiplier = 1f;
- public float xRotation;
- //Variables for input
- float x, y;
- bool sprinting, jumping;
- private Vector3 playerScale;
- private Vector3 moveDirection = Vector3.zero;
- private bool readyToJump = true;
- private float jumpCooldown = 0.25f;
- public float jumpForce = 550f;
- // Start is called before the first frame update
- void Start()
- {
- //characterController = GetComponent<CharacterController>();
- playerScale = transform.localScale;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- void Awake()
- {
- rb = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- private void Update()
- {
- MyInput();
- Look();
- }
- private void FixedUpdate()
- {
- Movement();
- }
- private void MyInput()
- {
- x = Input.GetAxisRaw("Horizontal");
- y = Input.GetAxisRaw("Vertical");
- jumping = Input.GetButton("Jump");
- }
- private void Movement()
- {
- /*
- if (characterController.isGrounded)
- {
- //This applies movement
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
- moveDirection *= moveSpeed;
- if (Input.GetButton("Jump"))
- {
- moveDirection.y = 5.0f;
- }
- }
- characterController.Move(moveDirection * Time.deltaTime);
- */
- //moveDirection.y -= gravity * Time.deltaTime;
- //This the new formula for movement
- rb.AddForce(Vector3.down * Time.deltaTime * 10);
- // This calls method and Finds velocity to where player is looking
- Vector2 mag = FindVelRelativeToLook();
- float xMag = mag.x, yMag = mag.y;
- float maxSpeed = this.maxSpeed;
- //If the speed is larget than maxspeed, it will cancel out the input so you don't go over max speed.
- if (x > 0 && xMag > maxSpeed) x = 0;
- if(x < 0 && xMag < -maxSpeed) x = 0;
- if(y > 0 && yMag > maxSpeed) y = 0;
- if(y < 0 && yMag < -maxSpeed) y = 0;
- float mutiplier = 1f, mutiplierV = 1f;
- if (!grounded)
- {
- mutiplier = 0.5f;
- mutiplierV = 0.5f;
- }
- //Apply forces to move player
- rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * mutiplier * mutiplierV);
- rb.AddForce(orientation.transform.forward * x * moveSpeed * Time.deltaTime * mutiplier);
- }
- public Vector2 FindVelRelativeToLook()
- {
- float lookAngle = orientation.transform.eulerAngles.y;
- float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
- float u = Mathf.DeltaAngle(lookAngle, moveAngle);
- float v = 90 - u;
- float magnitue = rb.velocity.magnitude;
- float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
- float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
- //This go back to void Movemement() and return math formulas
- return new Vector2(xMag, yMag);
- }
- //This will control our camera movement
- private float desiredX;
- private void Look()
- {
- float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
- float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
- //find current look rotation and location
- Vector3 rot = playerCam.transform.localRotation.eulerAngles;
- desiredX = rot.y + mouseX;
- // this will lock camera rotation
- xRotation -= mouseY;
- xRotation = Mathf.Clamp(xRotation, -90f, 90f);
- playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
- orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
- }
- private void OnCollisionStay(Collision other)
- {
- int layer = other.gameObject.layer;
- if (whatIsGround != (whatIsGround | (1 << layer))) return;
- for (int i = 0; i < other.contactCount; i++)
- {
- Vector3 normal = other.contacts[i].normal;
- //THIS IS FLOOR!
- if (isFloor(normal))
- {
- grounded = true;
- cancellingGrounded = false;
- // normalVector = normal;
- CancelInvoke(nameof(StopGrounded));
- }
- }
- float delay = 3f;
- if (!cancellingGrounded)
- {
- cancellingGrounded = true;
- Invoke(nameof(StopGrounded), Time.deltaTime * delay);
- }
- }
- private bool cancellingGrounded;
- private bool isFloor(Vector3 v)
- {
- float angle = Vector3.Angle(Vector3.up, v);
- return angle < maxSlopeAngle;
- }
- private void StopGrounded()
- {
- grounded = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement