My code for a game that I’m creating is has a jump logic. But in playmode my jump is going backwards instead of just going straight into the Y
axis. I don’t understand why. Every time I jump my character goes backwards while in the air jumping and lands backwards of his original position. sorry for bad english. This is my code please help and ignore the comments please they are in portuguese!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class AnimationAndMovementController : MonoBehaviour
{
PlayerInput playerInput;
CharacterController characterController;
Animator animator;
int isWalkingHash;
int isRunningHash;
Vector2 currentMovementInput;
Vector3 currentMovement;
Vector3 currentRunMovement;
Vector3 appliedMovement;
bool isMovementPressed;
bool isRunPressed;
float rotationFactorPerFrame = 15.0f;
float runMultiplier = 3.0f;
// variáveis de gravidade
float groundedGravity = -.05f;
float gravity = -9.8f;
// variáveis do Jump
bool isJumpPressed = false;
float initialJumpVelocity;
float maxJumpHeight = 4.0f;
float maxJumpTime = 0.75f;
bool isJumping = false;
int isJumpingHash;
bool isJumpingAnimating = false;
private void Awake()
{
playerInput = new PlayerInput();
characterController = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
isWalkingHash = Animator.StringToHash("isWalking");
isRunningHash = Animator.StringToHash("isRunning");
isJumpingHash = Animator.StringToHash("isJumping");
playerInput.CharacterControls.Move.started += onMovementInput;
playerInput.CharacterControls.Move.canceled += onMovementInput;
playerInput.CharacterControls.Move.performed += onMovementInput;
playerInput.CharacterControls.Run.started += onRun;
playerInput.CharacterControls.Run.canceled += onRun;
playerInput.CharacterControls.Jump.started += onJump;
playerInput.CharacterControls.Jump.canceled += onJump;
}
void onJump (InputAction.CallbackContext context)
{
isJumpPressed = context.ReadValueAsButton();
}
void onRun (InputAction.CallbackContext context)
{
isRunPressed = context.ReadValueAsButton();
}
void handleRotation()
{
Vector3 positionToLookAt;
positionToLookAt.x = currentMovement.x;
positionToLookAt.y = 0.0f;
positionToLookAt.z = currentMovement.z;
Quaternion currentRotation = transform.rotation;
if (isMovementPressed)
{
Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
}
}
void onMovementInput (InputAction.CallbackContext context)
{
currentMovementInput = context.ReadValue<Vector2>();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
currentRunMovement.x = currentMovementInput.x * runMultiplier;
currentRunMovement.z = currentMovementInput.y * runMultiplier;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
setupJumpVariables();
}
void setupJumpVariables()
{
float timeToApex = maxJumpTime / 2;
gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
}
void handleJump()
{
if (!isJumping && characterController.isGrounded && isJumpPressed)
{
// colocar animator "Jump"
animator.SetBool(isJumpingHash, true);
isJumpingAnimating = true;
isJumping = true;
currentMovement.y = initialJumpVelocity;
appliedMovement.y = initialJumpVelocity;
}
else if (!isJumpPressed && isJumping && characterController.isGrounded)
{
isJumping = false;
}
}
void handleAnimation()
{
bool isWalking = animator.GetBool(isWalkingHash);
bool isRunning = animator.GetBool(isRunningHash);
if (isMovementPressed && !isWalking)
{
animator.SetBool(isWalkingHash, true);
}
else if (!isMovementPressed && isWalking)
{
animator.SetBool(isWalkingHash, false);
}
if ((isMovementPressed && isRunPressed) && !isRunning)
{
animator.SetBool(isRunningHash, true);
}
else if ((!isMovementPressed || !isRunPressed) && isRunning)
{
animator.SetBool(isRunningHash, false);
}
}
void handleGravity()
{
// pra pulo dinamico
bool isFalling = currentMovement.y <= 0.0f || !isJumpPressed;
float fallMultiplier = 2.0f;
// isGrounded determinar gravidade no chão
if (characterController.isGrounded)
{
if (isJumpingAnimating)
{
animator.SetBool(isJumpingHash, false);
isJumpingAnimating = false;
}
// colocar animator "Jump" falso aqui
currentMovement.y = groundedGravity;
appliedMovement.y = groundedGravity;
}
// pra pulo dinamico
else if (isFalling)
{
float previousYVelocity = currentMovement.y;
currentMovement.y = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);
appliedMovement.y = Mathf.Max((previousYVelocity + currentMovement.y) * .5f, -20.0f);
}
else
{
float previousYVelocity = currentMovement.y;
currentMovement.y = currentMovement.y + (gravity * Time.deltaTime);
appliedMovement.y = (previousYVelocity + currentMovement.y) * .5f;
}
}
// Update is called once per frame
void Update()
{
handleRotation();
handleAnimation();
if (isRunPressed)
{
appliedMovement.x = currentRunMovement.x;
appliedMovement.z = currentRunMovement.z;
}
else
{
appliedMovement.x = currentMovement.x;
appliedMovement.z = currentMovement.z;
}
characterController.Move(appliedMovement * Time.deltaTime);
handleGravity();
handleJump();
}
private void OnEnable()
{
playerInput.CharacterControls.Enable();
}
private void OnDisable()
{
playerInput.CharacterControls.Disable();
}
}