c# - 为什么我写的跳转代码不能使用 Unity3d?
问题描述
我刚开始用c#编码。我正在尝试使用 Unity3d。我修复了我的代码中的错误,我不再收到任何错误,但是我为跳转我的角色而编写的代码不起作用,我的角色也没有跳转。谁能帮我 ?
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
CharacterController controller;
Vector3 velocity;
Rigidbody rb;
bool isGrounded;
public Transform ground;
public float distance = 0.3f;
public float speed;
public float jump;
public float jumpHeight;
public float gravity;
public LayerMask mask;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
#region Movement
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 move = transform.right * horizontal + transform.forward * vertical;
controller.Move(move*speed * Time.deltaTime);
#endregion
#region Jump
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);
}
#endregion
#region Gravity
isGrounded = Physics.CheckSphere(ground.position, distance,mask);
if (isGrounded && velocity.y <0)
{
velocity.y = 0f;
}
velocity.y += gravity + Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
#endregion
}
}~~~
解决方案
好像你有错字。在第 47 行,它可能应该是
rb.velocity = Vector3.up*Time.deltaTime*jump;
deltaTime 之后有一个额外的点。
推荐阅读
- url - elm 应用程序不断更改 init url
- salesforce - 如何将 sfdc.com 的 salesforce 组件导出到我的本地?
- flutter - 用户单击按钮时如何在列表中添加文本字段?
- javascript - IE 11:对象不支持属性或方法“getElementsByClassName”
- python - 如何使用python无限播放“.wav”文件
- docker - 在 ubuntu 的 Dockerfile 中安装特定版本的 python
- java - 从另一个类android调用方法时出现问题?
- python - python psutil.net_io_counters() 返回无效结果
- node.js - 在cmd中使用node.js执行'tracert',但我的输出不一致
- java - 读取 HttpServletRequest 有效负载