Unity3d 统一游戏,我正在尝试写一个无止境的游戏在太空中
我是Unity的初学者,我会加上数学。钳制功能在我的游戏中,但添加此功能后,我的飞船不会像runner无尽游戏中那样向前移动:Unity3d 统一游戏,我正在尝试写一个无止境的游戏在太空中,unity3d,Unity3d,我是Unity的初学者,我会加上数学。钳制功能在我的游戏中,但添加此功能后,我的飞船不会像runner无尽游戏中那样向前移动: public class MovePlayer: MonoBehaviour { public float xmin, xmax; Rigidbody player; float speed=5f; // Start is called before the first frame update void Start(
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);
}
}
公共类MovePlayer:MonoBehavior
{
公共浮点数xmin,xmax;
刚体运动员;
浮动速度=5f;
//在第一帧更新之前调用Start
void Start()
{
player=GetComponent();
}
//每帧调用一次更新
无效更新()
{
float horizontal=Input.GetAxis(“水平”);
player.velocity=新矢量3(水平*速度,0,速度);
var clampedPositionX=Mathf.Clamp(player.position.x,xmin,xmax);
player.position=新矢量3(夹紧位置X,0,速度);
}
}
您是否尝试过player.position+=新矢量3(0,0,速度)代码>?之后,如果要设置“固定”的x位置,只需编写player.position=new Vector3(clampedPositionX,player.position.y,player.position.z)