Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Update()
- {
- GetInput();
- ProcessInput();
- }
- void GetInput()
- {
- // Get the input from the Rewired Player. All controllers that the Player owns will contribute, so it doesn't matter
- // whether the input is coming from a joystick, the keyboard, mouse, or a custom controller.
- horizontalMove = rewiredPlayer.GetAxis("Move Horizontal"); // get input by name or action id
- verticalMove = rewiredPlayer.GetAxis("Move Vertical");
- jumpPressed = rewiredPlayer.GetButtonDown("Jump");
- meleeAttackPressed = rewiredPlayer.GetButtonDown("Melee Attack");
- }
- void ProcessInput()
- {
- isGrounded = IsGrounded();
- if (isGrounded && playerVelocity.y < 0)
- {
- playerVelocity.y = 0;
- }
- Vector3 move = new Vector3(horizontalMove, playerVelocity.y, verticalMove);
- if (move.magnitude >= 0.1f)
- {
- // Custom code to make player rotate and walk using camera's forward axis
- float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg + mainCam.eulerAngles.y;
- float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
- transform.rotation = Quaternion.Euler(0, angle, 0);
- moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
- }
- characterController.Move(moveDirection * moveSpeed * Time.deltaTime);
- if (jumpPressed)
- {
- if (isGrounded)
- {
- Jump();
- jumpPressed = false;
- }
- else if (State == PlayerState.JUMP)
- {
- DoubleJump();
- }
- }
- ApplyGravity();
- characterController.Move(playerVelocity * Time.deltaTime);
- }
- void Jump()
- {
- State = PlayerState.JUMP;
- playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravity);
- }
- void DoubleJump()
- {
- State = PlayerState.DOUBLE_JUMP;
- playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravity);
- }
- void ApplyGravity()
- {
- playerVelocity.y += gravity * Time.deltaTime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement