Unity3d 如何计算目标目的地

Unity3d 如何计算目标目的地,unity3d,position,transform,renderer,qvector3d,Unity3d,Position,Transform,Renderer,Qvector3d,我弄明白这个有点困难。我想达到的是一种攻势。运动员从远处向目标冲刺 该图显示了设置。蓝钻石是玩家,红钻石是目标。紫色框是目标SkinnedMesh渲染器的渲染器边界。我使用渲染器边界是因为某些目标的网格比其他目标的网格大得多。目前,玩家正在向橙色的星星射击……这是不现实的。我希望他,无论目标面对什么方向,始终瞄准目标相对于其位置的最近点……在图表中,这将是棕色星。这是我一直在使用的代码 public IEnumerator Blitz() { rigidbody.velocity =

我弄明白这个有点困难。我想达到的是一种攻势。运动员从远处向目标冲刺

该图显示了设置。蓝钻石是玩家,红钻石是目标。紫色框是目标SkinnedMesh渲染器的渲染器边界。我使用渲染器边界是因为某些目标的网格比其他目标的网格大得多。目前,玩家正在向橙色的星星射击……这是不现实的。我希望他,无论目标面对什么方向,始终瞄准目标相对于其位置的最近点……在图表中,这将是棕色星。这是我一直在使用的代码

 public IEnumerator Blitz()
{
    rigidbody.velocity = Vector3.zero; //ZERO OUT THE RIGIDBODY VELOCITY TO GET READY FOR THE BLITZ
    SkinnedMeshRenderer image = target.GetComponentInChildren<SkinnedMeshRenderer>();
    Vector3 position = image.renderer.bounds.center + image.renderer.bounds.extents;
    position.y = target.transform.position.y;
    while(Vector3.Distance(transform.position, position) > 0.5f)
    {
    transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * 10);
        yield return null;
    }
    Results(); //IRRELEVANT TO THIS PROBLEM. THIS CALCULATES DAMAGE.
    Blitz.Stop(); //THE PARTICLE EFFECT ASSOCIATED WITH THE BLITZ.
    GetComponent<Animator>().SetBool(moveName, false); //TRANSITIONS OUT OF THE BLITZ ANIMATION
    GetComponent<Input>().NotAttacking(); //LET'S THE INPUT SCRIPT KNOW THE PLAYER CAN HAVE CONTROL BACK.
}
有了方向,你可以做很多事情。例如,你可以将方向乘以你的速度,然后将其加到你的位置上

transform.position += Direction * MoveSpeed;
如果要到达最近的点,可以使用方便的Collider.ClosestPointOnBounds方法

Target = TargetObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position)
对于在目标点停止,可以使用到达行为

//Declare the distance to start slowing down
float ClosingDistance = Speed * 2;
//Get the distance to the target
float Distance = (Target - transform.position).magnitude;
//Check if the player needs to slow down
if (Distance < ClosingDistance)
{
//If you're closer than the ClosingDistance, move slower
transform.position += Direction * (MoveSpeed * Distance / ClosingDistance);
}
else{
//If not, move at normal speed
transform.position += Directino * MoveSpeed;
}

我已经在用Vector3.Lerp。。。在我代码的第9行。对撞机。我不知道的最接近点边界…听起来很棒!然而,问题仍然在于停在正确的位置以模拟玩家与目标的碰撞。这就是为什么我还需要使用renderer.bounds。在动画中,碰撞器不会改变,至少在我的例子中不会,但网格会改变。。。因此,如果玩家身体前倾,他就在对撞机外面。为了弥补这一点,我想将renderer.bounds合并到计算中。在正确的位置停止是指当玩家站在目标位置时停止?如果是这样,插值将为您实现这一点。你也可以使用到达行为,我会在答案中添加解释。
transform.position = Vector3.Lerp(Target,transform.position,time.DeltaTime);
//Declare the distance to start slowing down
float ClosingDistance = Speed * 2;
//Get the distance to the target
float Distance = (Target - transform.position).magnitude;
//Check if the player needs to slow down
if (Distance < ClosingDistance)
{
//If you're closer than the ClosingDistance, move slower
transform.position += Direction * (MoveSpeed * Distance / ClosingDistance);
}
else{
//If not, move at normal speed
transform.position += Directino * MoveSpeed;
}