unity fps jump with character controller 角色控制器

unity fps jump with character controller

我对 unity 还很陌生,想创建一个 fps 运动。 我从 Brackeys 找到了这个教程,它的效果相对较好,但如果你在头顶上跳下天花板,你就会飞一会儿。很抱歉问这样一个基本问题,但我找不到任何东西。这里有一些代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;
    Animator animator;

    public float speed = 7f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (x != 0)
        {
            animator.SetBool("WalkXTrigger", true);
            x /= 2;
        }
        else
        {
            animator.SetBool("WalkXTrigger", false);
        }

        if (z != 0)
        {
            if (z<0)
            {
                animator.SetBool("WalkBackTrigger", true);
                z /= 1.5f;
            }
            if (z>0)
            {
                animator.SetBool("WalkTrigger", true);
            }
        }
        else
        {
            animator.SetBool("WalkTrigger", false);
            animator.SetBool("WalkBackTrigger", false);
        }

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

        controller.Move(move * speed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
            animator.SetBool("JumpTrigger", true);
        }

        else
        {
            animator.SetBool("JumpTrigger", false);
        }

        velocity.y += gravity * Time.deltaTime;

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

所以我认为有两种选择: 1.角色撞到天花板后向下移动 2.测量角色和天花板之间的space并设置为等于跳跃高度 我的问题:我不知道该怎么做 希望我的英语没问题,问题描述得足够多 感谢您的帮助

你在更新循环结束时有这个所以我假设你的角色总是在飞?

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

继续编码和练习,你会找到答案的!

我不能 100% 确定您的角色是继续飞行,还是只停留一两秒钟,但无论哪种方式,这一小段代码都能正常工作。

public GameObject ceiling; //set this to your ceiling in the editor.
public float ceilingDown = 0.5; //if this value doesn't work, just mess around with it a little.

void OnTriggerEnter(ceiling){
    velocity.y -= ceilingDown;
}

如果将此添加到您的代码中,它应该会迫使播放器稍微向下。重力将在此之后接管。

我不太清楚为什么,但这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {

public CharacterController controller;

public float speed = 12f;
public float speedy;
public float speedOffset = 3f;
public float gravity = -9.81f;
public float jumpHeight = 3f;

public float playerHeight = 1.6f;
public float groundDistance = 0.4f;


Vector3 velocity;
bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
    
    RaycastHit hit;
    Ray downRay = new Ray(transform.position, -Vector3.up);

    if (Physics.Raycast(downRay, out hit))
    {
        if (hit.distance >= playerHeight + groundDistance)
        {
            isGrounded = false;
        }
        else
        {
            isGrounded = true;
        }

            
    }
    
    
    if (isGrounded)
    {
        speedy = speed/(1 + speedOffset);
    }
    else
    {
        speedy = speed;
    }
    
    if(isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }
    
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    
    Vector3 move = transform.right * x + transform.forward * z;
    
    controller.Move(move * speedy * Time.deltaTime);
    
    if(Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }
    
    velocity.y += gravity * Time.deltaTime;

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

我不知道,我是一个没有经验的 9 岁孩子,所以试试这个,看看它是否有效

我来晚了,但这段代码只适用于角色控制器。如果您没有接地(在空中),它会检查您是否与上方的物体发生碰撞。如果有,它会降低你的 y 速度,让你立即下降。把它想象成你撞到了你的头。

>         if (!isGrounded && (controller.collisionFlags & CollisionFlags.Above) != 0)
>         {
>             velocity.y = -velocity.y;
>         }