I made a controller responsible for the movement of the character. Movement is implemented using RigidBody. But there was such a problem: when I jump and hit the wall in the air, I kind of stick to it and don't fall to the ground, I can even walk left or right on it. Can you tell me how to fix this.
Here is the code responsible for movement:
using UnityEngine;
public class FPSInput2 : MonoBehaviour
{
public float speed = 6f;
public float jumpForce = 5f;
Rigidbody rb;
bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
void Update()
{
}
private void FixedUpdate()
{
HandleMove();
CheckGround();
HandleJump();
}
void HandleMove()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
Vector3 moveDirection = (transform.right * moveX + transform.forward * moveZ).normalized;
Vector3 velocity = rb.linearVelocity;
if (moveX != 0 || moveZ != 0)
{
velocity.x = moveDirection.x * speed;
velocity.z = moveDirection.z * speed;
}
else
{
if (isGrounded)
{
velocity.x = Mathf.Lerp(velocity.x, 0, Time.deltaTime * speed * 0.5f);
velocity.z = Mathf.Lerp(velocity.z, 0, Time.deltaTime * speed * 0.5f);
}
}
rb.linearVelocity = velocity;
}
void HandleJump()
{
if (Input.GetAxis("Jump") > 0 && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
void CheckGround()
{
isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f);
}
}
Here are all the components on the character:
I made a controller responsible for the movement of the character. Movement is implemented using RigidBody. But there was such a problem: when I jump and hit the wall in the air, I kind of stick to it and don't fall to the ground, I can even walk left or right on it. Can you tell me how to fix this.
Here is the code responsible for movement:
using UnityEngine;
public class FPSInput2 : MonoBehaviour
{
public float speed = 6f;
public float jumpForce = 5f;
Rigidbody rb;
bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
void Update()
{
}
private void FixedUpdate()
{
HandleMove();
CheckGround();
HandleJump();
}
void HandleMove()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
Vector3 moveDirection = (transform.right * moveX + transform.forward * moveZ).normalized;
Vector3 velocity = rb.linearVelocity;
if (moveX != 0 || moveZ != 0)
{
velocity.x = moveDirection.x * speed;
velocity.z = moveDirection.z * speed;
}
else
{
if (isGrounded)
{
velocity.x = Mathf.Lerp(velocity.x, 0, Time.deltaTime * speed * 0.5f);
velocity.z = Mathf.Lerp(velocity.z, 0, Time.deltaTime * speed * 0.5f);
}
}
rb.linearVelocity = velocity;
}
void HandleJump()
{
if (Input.GetAxis("Jump") > 0 && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
void CheckGround()
{
isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f);
}
}
Here are all the components on the character:
Share Improve this question edited Mar 20 at 10:23 Velocity asked Mar 20 at 9:27 VelocityVelocity 212 bronze badges 3 |1 Answer
Reset to default 0Try using Debug.Log("isGrounded: + isGrounded")
to check what the value is when you collide with the wall. Also, check that your raycast isn't colliding with the player itself. Its hard to say what's happening as we can't see what other components are on your player, so its worth posting a screenshot of your inspector along with sharing your code for future reference.
If I had to take a guess, you have a collider on your player and your raycast is colliding with it so its always returning true. Check to see if that's the case, if it is then you'll want to use a layermask to have your raycast ignore the player. The documentation for raycasts shows how to pass in a layermask to the raycast.
You'll need to go to your player object in the inspector and near the top right you'll see a layers option. Add your player to a layer then you can have your raycast collide with all layers except your player.
isGrounded = true
. – Sinatr Commented Mar 20 at 9:33