首页 > 解决方案 > unity fps 跳跃与角色控制器

问题描述

我对统一很陌生,想创建一个 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);
    }
}

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

标签: unity3dcontrolsframe-rate

解决方案


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

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岁的孩子,所以试试这个看看它是否有效


推荐阅读