C# Unity3D刚体移动位置闪烁

C# Unity3D刚体移动位置闪烁,c#,unity3d,unity5,rigid-bodies,C#,Unity3d,Unity5,Rigid Bodies,我目前正在制作一款游戏,我正在使用“点击”在Unity中移动。当我单击地图上的某个点时,我将鼠标的单击设置为目标,然后使用游戏对象上的刚体使用rigidBody.MovePosition()移动它。当我这样做时,当游戏对象到达目的地时,我会得到很多闪烁。感谢您的帮助。谢谢 // COMPONENTS Rigidbody rigidBody; // MOVEMENT Vector3 destination; Vector3 direction; // Use this for init

我目前正在制作一款游戏,我正在使用“点击”在Unity中移动。当我单击地图上的某个点时,我将鼠标的单击设置为目标,然后使用游戏对象上的刚体使用rigidBody.MovePosition()移动它。当我这样做时,当游戏对象到达目的地时,我会得到很多闪烁。感谢您的帮助。谢谢

    // COMPONENTS
Rigidbody rigidBody;

// MOVEMENT
Vector3 destination;
Vector3 direction;

// Use this for initialization
void Start()
{
    rigidBody = GetComponent<Rigidbody>();
    destination = transform.position;
}

// Update is called once per frame
void Update()
{
    DetectInput();
}

void FixedUpdate()
{
    MoveControlledPlayer();
}

void MoveControlledPlayer()
{

    transform.LookAt(destination);
    Vector3 direction = (destination - transform.position).normalized;
    rigidBody.MovePosition(transform.position + direction * 5 * Time.deltaTime);
}

void DetectInput()
{
   if (Input.GetMouseButton(0))
    {
        SetDestination();
    }
}

void SetDestination()
{
    if (!EventSystem.current.IsPointerOverGameObject())
    {
    Plane field = new Plane(Vector3.up, transform.position);
    Ray ray;
    float point = 0;

    ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (field.Raycast(ray, out point))
        destination = ray.GetPoint(point);

     }
}
//组件
刚体刚体;
//运动
矢量3目的地;
矢量3方向;
//用于初始化
void Start()
{
刚体=GetComponent();
目的地=变换位置;
}
//每帧调用一次更新
无效更新()
{
检测输入();
}
void FixedUpdate()
{
MoveControlledPlayer();
}
void MoveControlledPlayer()
{
transform.LookAt(目的地);
Vector3方向=(目的地-transform.position);
刚体.MovePosition(transform.position+direction*5*Time.deltaTime);
}
void DetectInput()
{
if(输入。GetMouseButton(0))
{
SetDestination();
}
}
void SetDestination()
{
如果(!EventSystem.current.IsPointerOverGameObject())
{
平面场=新平面(矢量3.up,变换位置);
射线;
浮点数=0;
ray=Camera.main.ScreenPointToRay(输入.鼠标位置);
if(场光线投射(光线,外点))
destination=ray.GetPoint(点);
}
}

当使用基于速度的行为时,到达某个点非常困难,并且经常会导致闪烁:这是因为对象总是经过其目的地


解决这个问题的一种方法是当物体离目标足够近时停止运动。

我用临时关节做这种运动。它们非常精确/可配置/可嵌入

在2D中,我使用a来控制刚体点之间的距离,或实体和世界点之间的距离。在3D中,您可以使用或


然后,距离之间的距离基本上与您现在移动每帧的方式相同(在
FixedUpdate
)。

您的目的地是一个点。所以很难达到完全相同的位置。当您的目的地离您的对象非常非常近时,您应该添加一些代码来管理案例。谢谢。这对我很管用。简单易修复。非常感谢。