我在联合制作SpaceShooter游戏。
这是函数。
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Player.GetComponent<Rigidbody>().velocity=movement*speed;
Player.GetComponent<Rigidbody> ().rotation = Quaternion.Euler(0.0f,0.0f,Player.GetComponent<Rigidbody> ().velocity.x * -tilt);
}在上面的代码中,当我添加夹紧功能来限制船舶的边界时,船的大小会从侧面减小。这是减少船舶尺寸的钳位功能代码。
Player.GetComponent<Rigidbody> ().position=new Vector3
(
Mathf.Clamp(Player.GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(Player.GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);以前的船号:

船号后:

我在夹紧功能中精确地给出了屏幕的边界值。
发布于 2017-09-26 08:23:50
修改刚体的位置确实会移动转换,但是在最后的物理步骤之后。虽然它被认为是更快的,但它可能会引起一些不想要的行为,因为你正在移动一些发动机的物理也在使用的东西,并在FixedUpdate函数中这样做,这应该主要用于物理物质。将Player.GetComponent<Rigidbody>().position替换为Player.GetComponent<Transform>().position。另外,由于这里不使用任何物理,而是直接修改对象位置,所以请在Update函数中设置代码的这一部分。
void Update()
{
Transform playerTransform = Player.GetComponent<Transform>();
playerTransform.position=new Vector3
(
Mathf.Clamp(playerTransform.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(playerTransform.position.z, boundary.zMin, boundary.zMax)
);
}如果您想使用刚体移动对象,请不要这样做。考虑使用MovePosition函数。移动它的方式使它传送,而不是计算在同一帧上的碰撞,而是在下一个帧上。
另外,您应该在类中保存对播放机转换的引用,这样就不必每次都使用GetComponent函数。
发布于 2017-09-26 12:51:42
通过将背景“四”推到飞机下方(约-4),并将船的Y值设置为0,解决了这个问题。
https://stackoverflow.com/questions/46421202
复制相似问题