Advertisement
Guest User

Character Controller

a guest
Mar 14th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1.     void Update()
  2.     {
  3.         GetInput();
  4.         ProcessInput();
  5.     }
  6.  
  7.  
  8.  
  9.     void GetInput()
  10.     {
  11.         // Get the input from the Rewired Player. All controllers that the Player owns will contribute, so it doesn't matter
  12.         // whether the input is coming from a joystick, the keyboard, mouse, or a custom controller.
  13.         horizontalMove = rewiredPlayer.GetAxis("Move Horizontal"); // get input by name or action id
  14.         verticalMove = rewiredPlayer.GetAxis("Move Vertical");
  15.         jumpPressed = rewiredPlayer.GetButtonDown("Jump");
  16.         meleeAttackPressed = rewiredPlayer.GetButtonDown("Melee Attack");
  17.     }
  18.  
  19.     void ProcessInput()
  20.     {
  21.         isGrounded = IsGrounded();
  22.  
  23.         if (isGrounded && playerVelocity.y < 0)
  24.         {
  25.             playerVelocity.y = 0;
  26.         }
  27.  
  28.         Vector3 move = new Vector3(horizontalMove, playerVelocity.y, verticalMove);
  29.         if (move.magnitude >= 0.1f)
  30.         {
  31.             // Custom code to make player rotate and walk using camera's forward axis
  32.             float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg + mainCam.eulerAngles.y;
  33.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
  34.             transform.rotation = Quaternion.Euler(0, angle, 0);
  35.  
  36.             moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
  37.         }
  38.  
  39.         characterController.Move(moveDirection * moveSpeed * Time.deltaTime);
  40.  
  41.  
  42.         if (jumpPressed)
  43.         {
  44.             if (isGrounded)
  45.             {
  46.                 Jump();
  47.                 jumpPressed = false;
  48.             }
  49.             else if (State == PlayerState.JUMP)
  50.             {
  51.                 DoubleJump();
  52.             }
  53.         }
  54.  
  55.         ApplyGravity();
  56.         characterController.Move(playerVelocity * Time.deltaTime);
  57.  
  58.     }
  59.  
  60.     void Jump()
  61.     {
  62.         State = PlayerState.JUMP;
  63.         playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravity);
  64.     }
  65.  
  66.     void DoubleJump()
  67.     {
  68.         State = PlayerState.DOUBLE_JUMP;
  69.         playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravity);
  70.     }
  71.  
  72.     void ApplyGravity()
  73.     {
  74.         playerVelocity.y += gravity * Time.deltaTime;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement