为什么刚体播放器控制器不工作?我无法移动播放器



这个脚本附加到我的播放器上,该播放器有一个刚体组件。刚体使用重力并且是运动学的。

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可以更改运动学刚体的位置。

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

是2d游戏还是3d游戏?我对3d游戏不太了解,但那个代码似乎就像一个2d游戏控制器,如果我错了,告诉我,我可以帮助你

最新更新