最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - A bug with the character sticking to the wall - Stack Overflow

programmeradmin2浏览0评论

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
  • Put breakpoint when character stick to the wall and check if e.g. isGrounded = true. – Sinatr Commented Mar 20 at 9:33
  • Unity doesn't make it easy to use breakpoints, but Sinatrs idea still stands. Try using Debug.Log(isGrounded) for the value to be printed to the Console in Unity – Istalri Skolir Commented Mar 20 at 9:59
  • when I stick to the wall isGrounded == false – Velocity Commented Mar 20 at 10:25
Add a comment  | 

1 Answer 1

Reset to default 0

Try 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.

发布评论

评论列表(0)

  1. 暂无评论