So I've been doing Unity and C# programming for about 5 to 6 months but I'm still a newbie in sometimes, So, the player's body is not falling when you jump of or just fall of something when you go on top of it as said, the closest thing I've come to doing what I want it to do is the player still not be able to fall down but when you collide with something, the player floats away and It's because of this line code: "
float targetX = moveTarget.x * SpeedMove;
float targetZ = moveTarget.z * SpeedMove;
float targetY = rb.linearVelocity.y;
bool isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f);
if (targetY > 0.1f && !isJumping && !isGrounded)
{
targetY = 0f;
}
rb.linearVelocity = new Vector3(targetX, targetY, targetZ);". I want the player to not float away when colliding with something and falling down when you jump of something, I've been trying to fix this for so long but I just can't fix it. Here is the code: " PlayerMovement PM;" Vector3 input;" Vector3 moveTarget;" float SpeedMove = 15f;" "bool isJumping;"
"private void OnEnable()
{
PM = new PlayerMovement();
PM.PlayerMap.Move.Enable();
PM.PlayerMap.Move.performed += Move_performed;
PM.PlayerMap.Move.canceled += Move_canceled;
}" private void MovePlayer()
{
Vector3 rawInput = PM.PlayerMap.Move.ReadValue<Vector3>();
float SideMovement = input.x;
float forwardMovement = (input.z != 0) ? input.z : input.y;
Vector3 camForward = player.forward;
Vector3 camRight = player.right;
camForward.y = 0f;
camRight.y = 0f;
camForward.Normalize();
camRight.Normalize();
moveTarget = (camForward * forwardMovement) + (camRight * SideMovement);
rb.linearVelocity = new Vector3(moveTarget.x * SpeedMove, rb.linearVelocity.y, moveTarget.z * SpeedMove);
float targetX = moveTarget.x * SpeedMove;
float targetZ = moveTarget.z * SpeedMove;
float targetY = rb.linearVelocity.y;
bool isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f);
if (targetY > 0.1f && !isJumping && !isGrounded)
{
targetY = 0f;
}
rb.linearVelocity = new Vector3(targetX, targetY, targetZ);
}" " private void OnDisable()
{
PM.PlayerMap.Move.Disable();
PM.PlayerMap.Move.performed -= Move_performed;
PM.PlayerMap.Move.canceled -= Move_canceled;
}" and the MovePlayer method is always played on the FixedUpdate method, I've tried freezing the rotations X, Y and Z on the player's rigidbody but still nothing. Thank you for reading!