JojikYT

Character

Dec 18th, 2021 (edited)
6,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class Character : MonoBehaviour
  4. {
  5.     [Header("Controls")]
  6.     public float playerSpeed = 5.0f;
  7.     public float crouchSpeed = 2.0f;
  8.     public float sprintSpeed = 7.0f;
  9.     public float jumpHeight = 0.8f;
  10.     public float gravityMultiplier = 2;
  11.     public float rotationSpeed = 5f;
  12.     public float crouchColliderHeight = 1.35f;
  13.  
  14.     [Header("Animation Smoothing")]
  15.     [Range(0, 1)]
  16.     public float speedDampTime = 0.1f;
  17.     [Range(0, 1)]
  18.     public float velocityDampTime = 0.9f;
  19.     [Range(0, 1)]
  20.     public float rotationDampTime = 0.2f;
  21.     [Range(0, 1)]
  22.     public float airControl = 0.5f;
  23.  
  24.     public StateMachine movementSM;
  25.     public StandingState standing;
  26.     public JumpingState jumping;
  27.     public CrouchingState crouching;
  28.     public LandingState landing;
  29.     public SprintState sprinting;
  30.     public SprintJumpState sprintjumping;
  31.  
  32.     [HideInInspector]
  33.     public float gravityValue = -9.81f;
  34.     [HideInInspector]
  35.     public float normalColliderHeight;
  36.     [HideInInspector]
  37.     public CharacterController controller;
  38.     [HideInInspector]
  39.     public PlayerInput playerInput;
  40.     [HideInInspector]
  41.     public Transform cameraTransform;
  42.     [HideInInspector]
  43.     public Animator animator;
  44.     [HideInInspector]
  45.     public Vector3 playerVelocity;
  46.  
  47.  
  48.     // Start is called before the first frame update
  49.     private void Start()
  50.     {
  51.         controller = GetComponent<CharacterController>();
  52.         animator = GetComponent<Animator>();
  53.         playerInput = GetComponent<PlayerInput>();
  54.         cameraTransform = Camera.main.transform;
  55.  
  56.         movementSM = new StateMachine();
  57.         standing = new StandingState(this, movementSM);
  58.         jumping = new JumpingState(this, movementSM);
  59.         crouching = new CrouchingState(this, movementSM);
  60.         landing = new LandingState(this, movementSM);
  61.         sprinting = new SprintState(this, movementSM);
  62.         sprintjumping = new SprintJumpState(this, movementSM);
  63.  
  64.         movementSM.Initialize(standing);
  65.  
  66.         normalColliderHeight = controller.height;
  67.         gravityValue *= gravityMultiplier;
  68.     }
  69.  
  70.     private void Update()
  71.     {
  72.         movementSM.currentState.HandleInput();
  73.  
  74.         movementSM.currentState.LogicUpdate();
  75.     }
  76.  
  77.     private void FixedUpdate()
  78.     {
  79.         movementSM.currentState.PhysicsUpdate();
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment