c# - 为什么刚体播放器控制器不工作?我无法移动播放器
问题描述
该脚本附加到我的具有刚体组件的播放器上。刚体使用重力并且是运动学的被设置为ture。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RigidbodyPlayercontroller : MonoBehaviour
{
Rigidbody rb;
public float speed;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float mH = Input.GetAxis("Horizontal");
float mV = Input.GetAxis("Vertical");
rb.velocity = new Vector3(mH * speed, rb.velocity.y, mV * speed);
}
}
但是玩家没有移动它什么都不做。我尝试了速度值 1 和 100。
解决方案
设置velocity
运动学刚体的 不会产生任何影响。如文档所述,使用MovePosition更改运动学Rigidbody的位置。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController: MonoBehaviour
{
public float speed;
private float vertical;
private float horizontal;
private Rigidbody rb;
void Start()
{
rb = GetComponent < Rigidbody > ();
}
void Update()
{
vertical = Input.GetAxis("Vertical") * speed;
horizontal = Input.GetAxis("Horizontal") * speed;
}
void FixedUpdate()
{
rb.MovePosition(
transform.position +
transform.right * horizontal * Time.fixedDeltaTime +
transform.forward * vertical * Time.fixedDeltaTime
);
}
}
推荐阅读
- javascript - 高效的数组映射
- reactjs - 将 material-ui 中的活动选项卡从默认粉色更改为其他颜色
- umbraco - Umbraco 中基于代码的 Serilog 配置
- java - Spring MVC 每次都渲染相同的视图
- wordpress - 如何创建一个基于 Laravel 的 WordPress 插件
- python - 如何加载具有错误数据类型标志的 HDF5 文件?
- docker - 无法在子文件夹中构建 docker 容器
- google-chrome-extension - 与 Progressive Web Apps (PWA) 交互的 Chrome 扩展
- kotlin - Kotlin 无法通过反射访问委托方法:“java.lang.NoSuchMethodException”
- kerberos - 应如何部署 kerberos 委派