使用角色控制器进行统一fps跳跃

时间:2020-05-01 22:45:14

标签: unity3d controls frame-rate

我对团结还很陌生,想创建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.测量角色和天花板之间的空间,并将其设置为等于跳跃高度 我的问题:我不知道该怎么做 希望我的英语还可以,问题已经足够描述了 谢谢您的帮助

3 个答案:

答案 0 :(得分:1)

我不太确定为什么,但是这是我的代码

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岁的经验,所以请尝试这个,看看它是否有效

答案 1 :(得分:0)

您在更新循环的末尾拥有了这个,所以我假设您的角色一直在飞行吗?

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

继续进行编码和练习,您将会发现答案!

答案 2 :(得分:0)

我不确定您的角色是继续飞行还是只是停留一两秒,但无论哪种方式,这小段代码都行得通。

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;
}

如果将此代码添加到您的代码中,它将迫使播放器稍微向下移动。重力将接管一切。