我是Unity的初学者,我添加了Math。我的游戏中具有钳位功能,但是添加此功能后,我的飞船不会像无尽的跑步者中那样前进:
public class MovePlayer: MonoBehaviour
{
public float xmin, xmax;
Rigidbody player;
float speed=5f;
// Start is called before the first frame update
void Start()
{
player = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
player.velocity = new Vector3(horizontal * speed, 0, speed);
var clampedPositionX = Mathf.Clamp(player.position.x, xmin, xmax);
player.position = new Vector3(clampedPositionX, 0, speed);
}
}