简体   繁体   English

Unity3D RigidBody MovePosition闪烁

[英]Unity3D RigidBody MovePosition Flicker

I currently am working on a game where I am using click to move in Unity. 我目前正在开发一款游戏,我正在使用Click来移动Unity。 When I click on a spot on the map, I set that mouse's click to the destination and then use the rigidBody on the gameobject to move it using RigidBody.MovePosition(). 当我点击地图上的某个位置时,我将鼠标的单击设置为目标,然后使用游戏对象上的rigidBody使用RigidBody.MovePosition()移动它。 When I do this, I am getting a lot of flicker when the gameobject reaches its destination. 当我这样做时,当游戏对象到达目的地时,我会得到很多闪烁。 Any assistance is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

    // 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);

     }
}

I do these kind of movements with temporary joints . 我做这些带有临时关节的动作。 They are extremely accurate / configurable / embeddable. 它们非常准确/可配置/可嵌入。

In 2D I use a DistanceJoint2D to control distance between rigidbody points, or between a body and a world point. 在2D中,我使用DistanceJoint2D来控制刚体点之间或身体与世界点之间的距离。 In 3D you could use SpringJoint or ConfigurableJoint . 在3D中,您可以使用SpringJointConfigurableJoint

Then just tween the distance basically the same way you do per frame moving now (on FixedUpdate ). 然后,补间距离基本上与每帧移动时的方式相同(在FixedUpdate )。

Reaching a point when using a velocity-based behavior is really hard, and often results in flickering: that's because the object is always passing over his destination. 使用基于速度的行为时达到一个点非常困难,并且经常导致闪烁:那是因为对象总是经过目的地。

A way to fix it is to stop the movement when the object is close enought to the target. 修复它的一种方法是在对象靠近目标时停止移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM