Question by
Crioyan · May 12 at 08:18 PM ·
scripting problemmovement
Slow falling
Hello,
I am a beginner with coding and followed a tutorial on YouTube for 3rd person movement with Unity's input manager by Sebastian Graves, in the tutorial we created scrips to move and animate a character but in the tutorial whenever the character is in the air movement is blocked. I found out that it was because of a boolean called "isInterancing", I manged to remove the if statement that blocks movement when the character is falling but when after doing that the character now falls too slow as shown below, I can't figure out why, so I hope someone can help me please.
public class PlayerLocomotion : MonoBehaviour
{
PlayerManager playerManager;
animatorManager animatorManager;
inputManager inputManager;
Vector3 moveDirection;
Transform cameraObject;
Rigidbody playerRigidbody;
[Header("Falling")]
public float inAirTimer;
public float leapingVelocity;
public float fallingVelocity;
public float rayCastHeightOffSet = 0.5f;
public LayerMask groundLayer;
[Header("Movement Flags")]
public bool isSprinting;
public bool isJumping;
public bool isGrounded;
[Header ("Movement Speeds")]
public float walkingSpeed = 1.5f;
public float runningSpeed = 5;
public float sprintingSpeed = 7;
public float rotationSpeed = 15;
[Header("Jump Speeds")]
public float jumpHeight = 3;
public float gravityIntensity = -15;
private void Awake()
{
playerManager = GetComponent<PlayerManager>();
animatorManager = GetComponent<animatorManager>();
inputManager = GetComponent<inputManager>();
playerRigidbody = GetComponent<Rigidbody>();
cameraObject = Camera.main.transform;
}
public void HandleAllMovement()
{
HandleFallingAndLanding();
if (playerManager.isInteracting)
return;
if (isJumping)
return;
HandleMovement();
HandleRotation();
}
private void HandleMovement()
{
if (isJumping)
return;
moveDirection = cameraObject.forward * inputManager.verticalInput;
moveDirection = moveDirection + cameraObject.right * inputManager.horizontalInput;
moveDirection.Normalize();
moveDirection.y = 0;
if(isSprinting)
{
moveDirection = moveDirection * sprintingSpeed;
}
else
{
if (inputManager.moveAmount >= 0.5f)
{
moveDirection = moveDirection * runningSpeed;
}
else
{
moveDirection = moveDirection * walkingSpeed;
}
}
Vector3 movementVelocity = moveDirection;
playerRigidbody.velocity = movementVelocity;
}
private void HandleRotation()
{
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 HandleFallingAndLanding()
{
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 * fallingVelocity * inAirTimer);
}
if(Physics.SphereCast(rayCastOrigin, 0.2f, -Vector3.up, out hit, groundLayer))
{
if (!isGrounded)
{
animatorManager.PlayTargetAnimation("Landing", true);
}
inAirTimer = 0;
isGrounded = true;
}
else
{
isGrounded = false;
}
}
public void HandleJumping()
{
if(isGrounded)
{
animatorManager.animator.SetBool("isJumping", true);
animatorManager.PlayTargetAnimation("Jumping", false);
float jumpingVelocity = Mathf.Sqrt(-2 * gravityIntensity * jumpHeight);
Vector3 playerVelocity = moveDirection;
playerVelocity.y = jumpingVelocity;
playerRigidbody.velocity = playerVelocity;
}
}
}
das564.gif
(237.8 kB)
Comment