-7
using UnityEngine; public class PlayerLocomotion : MonoBehaviour { PlayerManager playerManager; AnimatorManager animatorManager; InputManager inputManager; Vector3 moveDicrection; Transform cameraObject; Rigidbody playerRigidbody; [Header("Falling")] public float inAirTimer; public float leapingVelocity; public float fallingVelocicy; public float rayCastHeightOffSet = 0.5f; public LayerMask groundLayer; [Header("Movement Flags")] public bool isSprinting; public bool isGrounded; public bool isJumping; [Header("Movement Speeds")] public float walkingSpeed = 1.5f; public float runningSpeed = 5; public float sprintingSpeed = 7; public float rotationSpeed = 15; public float maxDistance = 1; [Header("Jump Speeds")] public float jumpHeight = 3; public float gravityIntensity = -15; public void Awake() { playerManager = GetComponent<PlayerManager>(); animatorManager = GetComponent<AnimatorManager>(); inputManager = GetComponent<InputManager>(); playerRigidbody = GetComponent<Rigidbody>(); cameraObject = Camera.main.transform; } public void HandleAllMovement() { HanleFallingandLAnding(); if (playerManager.isInteracting) return; HandleMovement(); HanleRotation(); } private void HandleMovement() { moveDicrection = cameraObject.forward * inputManager.verticalInput; moveDicrection = moveDicrection + cameraObject.right * inputManager.horizontalInput; moveDicrection.Normalize(); moveDicrection.y = 0; if (isSprinting) { moveDicrection = moveDicrection * sprintingSpeed; } else { if (inputManager.moveAmount >= 0.5f) { moveDicrection = moveDicrection * runningSpeed; } else { moveDicrection = moveDicrection * walkingSpeed; } } Vector3 targetPosition = transform.position + moveDicrection * Time.deltaTime; playerRigidbody.MovePosition(targetPosition); } private void HanleRotation() { if(isJumping) return ; Vector3 targetDirection = Vector3.zero; targetDirection = cameraObject.forward * inputManager.verticalInput; targetDirection = targetDirection + cameraObject.right * inputManager.horizontalInput; targetDirection.Normalize(); targetDirection.y = 0; if(targetDirection == Vector3.zero) targetDirection = transform.forward; Quaternion targetRotation = Quaternion.LookRotation(targetDirection); Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); transform.rotation = playerRotation; } private void HanleFallingandLAnding() { RaycastHit hit; Vector3 rayCastOrigin = transform.position; rayCastOrigin.y = rayCastOrigin.y + rayCastHeightOffSet; if (!isGrounded && !isJumping) { if(!playerManager.isInteracting) { animatorManager.PlayTargetAnimation("Falling", true); } inAirTimer = inAirTimer + Time.deltaTime; playerRigidbody.AddForce(transform.forward * leapingVelocity); playerRigidbody.AddForce(-Vector3.up * fallingVelocicy * inAirTimer); } if (Physics.SphereCast(rayCastOrigin, 0.2f, -Vector3.up, out hit, maxDistance, groundLayer)) { if (!isGrounded && !playerManager.isInteracting) { animatorManager.PlayTargetAnimation("Land", true); } inAirTimer = 0; isGrounded = true; } else { isGrounded = false; } } public void HandleJumping() { if (isGrounded) { animatorManager.animator.SetBool("isJumping", true); animatorManager.PlayTargetAnimation("Jump", false); float jumpingVelocity = Mathf.Sqrt(-2 * gravityIntensity * jumpHeight); Vector3 playerVelocity = moveDicrection; playerVelocity.y = jumpingVelocity; playerRigidbody.linearVelocity = playerVelocity; } } } 

I'm just learning unity few day ago, i don't understand why i can't move while jumping, i learning with tutorial form him: https://www.youtube.com/watch?v=kos9c32iWWA&list=PLD_vBJjpCwJsqpD8QRPNPMfVUpPFLVGg4&index=8, but something is wrong. Someone pls help me, i can't fix this problem with chatgpt...

3
  • 3
    Well .. you have a bool isJumping .. and then a check if(isJumping) return ; in HanleRotation and if (!isGrounded && !isJumping) { ... applies movement input } in HanleFallingandLAnding .. did ChatGPT not tell you that? Commented Nov 14 at 19:09
  • that combined with if (playerManager.isInteracting) return; and the hundreds of typos suggests you should take a step back, and read what you wrote. Understand the code, otherwise you following tutorials doesn't gain you anything. Blindly copying code - be it from a tutorial, StackOverflow or AI - won't get you very far tbh Commented Nov 18 at 6:55
  • Thanks for your help. About the spelling problem i am really sorry because english is not my first language. As for the logic problem i will delete this whole code and start from scratch, i really appreciate your help again thank you. Commented Nov 19 at 18:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.