Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player_Movement : MonoBehaviour {
- //Player Status
- public bool PlayerControl;
- //Dependencies
- private CharacterController controller;
- //PlayerMovement
- public float speed;
- public float walk_Speed;
- public float run_Speed;
- public float sprint_Speed;
- public float jumpSpeed;
- public float gravity = 5.0f;
- public float distanceToGround;
- public bool Grounded;
- private Vector3 moveDirection = Vector3.zero;
- //Sprinting
- public float SprintSmooth = .5f;
- void Start()
- {
- controller = GetComponent<CharacterController>();
- }
- void Update()
- {
- //If we can control the player then move
- if(PlayerControl)
- {
- Movement();
- DetectIfIsGrounded();
- }
- }
- void Movement()
- {
- if(Grounded)
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- if(Input.GetAxis("Horizontal") > .02f || Input.GetAxis("Horizontal") < -.02f || Input.GetAxis("Vertical") > .02f || Input.GetAxis("Vertical") < -.02f)
- {
- if(Input.GetButton("Sprint"))
- {
- speed = Mathf.Lerp(speed,sprint_Speed, SprintSmooth*10* Time.deltaTime);
- }
- else
- {
- speed = Mathf.Lerp(speed,run_Speed, SprintSmooth*10 * Time.deltaTime);
- }
- }
- else
- {
- speed = 0f;
- }
- if (Input.GetButton("Jump"))
- {
- moveDirection.y = jumpSpeed;
- }
- }
- else
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection.x *= speed;
- moveDirection.z *= speed;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- }
- void DetectIfIsGrounded () {
- if(distanceToGround <= 1.5f && controller.isGrounded)
- {
- Grounded = true;
- }
- else
- {
- Grounded = false;
- }
- RaycastHit hit = new RaycastHit();
- // LayerMask layerMask = ~(1 << LayerMask.NameToLayer("Player"));
- if (Physics.Raycast(transform.position, -Vector3.up, out hit))
- {
- distanceToGround = hit.distance;
- }
- // Grounded = Physics.Raycast(transform.position,-transform.up, out hit, 0.3f, layerMask);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement