c# - 在球体中设置玩家的边界

标签 c# unity3d boundary

我想限制玩家在球体中移动,示意图如下。如果玩家移动超出范围,则将玩家限制在球体最大半径范围内。

我如何编写 C# 代码来实现它,就像这样?

The player is at the center of a circle with a radius of 0.5. Anything beyond the circumference of the circle is out of bounds.

这些是我目前的步骤:

  1. 创建 3D 球体

  2. 创建附加到球体对象的 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/

相关文章:

c# - Unity C#无法从迭代器返回值。使用yield return语句返回一个值,或使用yield break结束迭代

node.js - Nock + 多部分表单数据 = 请求不匹配

java - 边界 Java 正则表达式不起作用

c# - 从另一个线程更新 Blazor 中的项目列表

c# - 当不存在 MBN 设备时,来自 IMbnInterfaceManager::GetInterfaces 的结果

c# - 如何检查 radComboboxItem 'CheckBox' ?

ios - 分段控件未附加到其边界

c# - 如何使用自定义泛型类型作为字典中的键?

c# - 麦克风的输出格式是什么以保存为Wav

c# - 我如何检测用户何时单击我的 slider 而不是统一滑动?