Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

c# - Jumping doesnt work after adding GroundCheck

public class PlayerMov : MonoBehaviour
{
    public CharacterController controller;
    public float Speed = 12f;
    public float gravity = -20;
    public float JumpH = 3f;
    public Transform groundCheck;
    public float groundDis = 0.4f;
    public LayerMask GroundMask;
    Vector3 Velocity;
    bool isGrounded;

    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDis, GroundMask);

        if (isGrounded && Velocity.y < 0 )
        {
            Velocity.y = -2;
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * Speed * Time.deltaTime);
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            Velocity.y = Mathf.Sqrt(JumpH * -2f * gravity);
        }
        Velocity.y += gravity * Time.deltaTime;

        controller.Move(Velocity * Time.deltaTime);
    }
}

If I change the += in Velocity.y += gravity * Time.deltaTime to a -=, I am staying on the ground but I am not able to jump. If I leave it, I am starting to float. I would be grateful if someone could help me solve this problem (I am trying to make a simple FP Controller using the CharacterController component).

question from:https://stackoverflow.com/questions/65872651/jumping-doesnt-work-after-adding-groundcheck

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...