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
- {
- CharacterController charControl;
- public float walkSpeed = 4;
- public float sprintSpeed;
- public bool IsSprinting;
- void Start()
- {
- charControl = GetComponent<CharacterController>();
- sprintSpeed = walkSpeed * 3;
- }
- void Update()
- {
- MovePlayer();
- }
- void MovePlayer()
- {
- float currentSpeed = walkSpeed;
- float horiz = Input.GetAxis("Horizontal");
- float vert = Input.GetAxis("Vertical");
- Vector3 moveDirSide = transform.right * horiz * currentSpeed;
- Vector3 moveDirForward = transform.forward * vert * currentSpeed;
- charControl.SimpleMove(moveDirSide + moveDirForward);
- IsSprinting = Input.GetKey(KeyCode.LeftShift);
- if (IsSprinting)
- {
- currentSpeed = sprintSpeed;
- }
- else
- {
- currentSpeed = walkSpeed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement