In unity 3d when I jump and my character is in mid air if I go into an object by pressing a/s/d/w from the right direction the character will stay stuck in the air as long as I hold it , how can I make it so that the character won't stay in mid air if I'm holding a movement button against an object without ruining the current movement system and variables amounts
here's my movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public Rigidbody rb;
public float MouseSensitivity;
public float MoveSpeed;
public float jumpForce;
public float fallMultiplayer = 2.0f;
// Smoothing factors
public float moveSmoothing = 0.1f;
public float rotateSmoothing = 0.1f;
//animator
public Animator anim;
bool isJumping = false;
//
private Vector3 targetMoveDirection;
private Quaternion targetRotation;
void Start()
{
//Cursor.visible = false;
}
void Update()
{
anim.SetBool("jump",false);
//Look around
Quaternion rotationDelta = Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0));
targetRotation = rb.rotation * rotationDelta;
rb.MoveRotation(Quaternion.Lerp(rb.rotation, targetRotation, rotateSmoothing));
//vertical parameter
anim.SetFloat("vertical moving", Input.GetAxis("Vertical"));
anim.SetFloat("horizontal", Input.GetAxis("Horizontal"));
//Move
targetMoveDirection = transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal");
rb.AddForce(targetMoveDirection * MoveSpeed, ForceMode.VelocityChange);
rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, moveSmoothing);
//Jump
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
{
isJumping = true;
}
else
{
isJumping = false;
}
if (Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.velocity.y) < 0.01f && !isJumping)
{
anim.SetBool("jump",true);
rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
rb.AddForce(Physics.gravity, ForceMode.Acceleration);
}
}
private void FixedUpdate()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * fallMultiplayer * Time.deltaTime;
}
}
}
If you do not wish to significantly modify the movement code then one possible solution may be to reduce the friction of the walls to 0 since the player character rigid body is most likely experiencing some friction with the walls causing it to stop falling.
Try creating a Physics3D
material, set its friction value to zero (or another sufficiently low value), and then make sure to assign this to your walls.