首页 > 解决方案 > 为什么我的重力不随时间变化?

问题描述

我正在使用unity 3d,并且我编写了一个脚本,理论上应该使我的速度在一段时间后下降,但它保持在一致的-1。为什么我的代码没有随着时间的推移降低速度?

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


public class gravity : MonoBehaviour
{
    public CharacterController controller;
    public float gravityy = -10f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    bool isGrounded;
    UnityEngine.Vector3 velocityy;

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

    // Update is called once per frame
    void Update()
    {
        velocityy.y += gravityy * Time.deltaTime;
        controller.Move(velocityy * Time.deltaTime);
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocityy.y < 0) ;
        {
            velocityy.y = -1f;
        }
    }
}

标签: unity3dgame-physics

解决方案


撇开我对您的代码片段的许多其他问题不谈,这可能不是isGrounded真的,因此velocityy.y被设置为-1f每个更新循环吗?

似乎您需要使用某种调试器。


推荐阅读