我想限制玩家在球体中移动,示意图如下。如果玩家移动超出范围,则将玩家限制在球体最大半径范围内。
我如何编写 C# 代码来实现它,就像这样?
这些是我目前的步骤:
创建 3D 球体
创建附加到球体对象的 C# 代码
到目前为止我的代码:
public Transform player;
void update(){
Vector3 pos = player.position;
}
最佳答案
我不知道你是如何计算你的玩家位置的,但是在将新位置分配给玩家之前,你应该检查并查看移动是否符合条件 检查距球心的新位置距离
//so calculate your player`s position
//before moving it then assign it to a variable named NewPosition
//then we check and see if we can make this move then we make it
//this way you don't have to make your player suddenly stop or move it
//back to the bounds manually
if( Vector3.Distance(sphereGameObject.transform.position, NewPosition)< radius)
{
//player is in bounds and clear to move
SetThePlayerNewPosition();
}
关于c# - 在球体中设置玩家的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41563066/