如何在虚幻蓝图中创建等效的Unity LookAt? [英] How can I create the equivalent of Unity LookAt in Unreal blueprint?

查看:165
本文介绍了如何在虚幻蓝图中创建等效的Unity LookAt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在虚幻中,我想要

  1. 旋转角色,使前进矢量指向目标的当前位置,
  2. 确保演员的向上向量与目标的向上向量相同

在Unity3D中,它非常简单.这是单行代码():

In Unity3D, it's super simple. It's a single line of code (source):

transform.LookAt(target, Vector3.up);

在蓝图中,有一个名为旋转查找"的节点.问题在于没有Up矢量参数,因此当您靠近目标位置时,可能会发生不必要的侧倾旋转.

In blueprint, there's a Node called "Find Look at Rotation". The problem is that there's no Up vector parameter, so when you are close to the target, you can have a unwanted roll rotation.

那么,如何在虚幻蓝图中创建等效的Unity LookAt?

So, how can I create the equivalent of Unity LookAt in Unreal blueprint?

推荐答案

我修改了在这里找到的解决方案:

I modified a solution I found here:

https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1482788-posting-the-source-code-for-lookrotation-for-those-谁需要-它

源演员正在用其z向量(虚幻中的Up向量)寻找目标.我将其更改为x向量,因为在虚幻中,正向向量是x轴.

The source actor was looking the target with his z vector (Up vector in Unreal). I changed it to be the x vector because in Unreal, the forward vector is the x axis.

lookAt参数是要查看的actor(目标)的位置. UpDirection是要与源的向上向量匹配的目标的向上向量.

The lookAt parameter is the position of the actor you want to look at (the target). UpDirection is the up vector of the target you want to match with the up vector of the source.

只需使用以下函数返回的FRotator值设置源的旋转,在源的C ++代码中调用该值即可:

Just set the rotation of the source with the FRotator value returned by the function below, called in the C++ code of the source:

FRotator MyLookRotation(FVector lookAt, FVector upDirection)
{
    FVector forward = lookAt - GetActorLocation();
    FVector up = upDirection;


   forward = forward.GetSafeNormal();
   up = up - (forward * FVector::DotProduct(up, forward));
   up = up.GetSafeNormal();

   ///////////////////////


   FVector vector = forward.GetSafeNormal();
   FVector vector2 = FVector::CrossProduct(up, vector);
   FVector vector3 = FVector::CrossProduct(vector, vector2);
   float m00 = vector.X;
   float m01 = vector.Y;
   float m02 = vector.Z;
   float m10 = vector2.X;
   float m11 = vector2.Y;
   float m12 = vector2.Z;
   float m20 = vector3.X;
   float m21 = vector3.Y;
   float m22 = vector3.Z;

   float num8 = (m00 + m11) + m22;
   FQuat quaternion = FQuat();

   if (num8 > 0.0f)
   {
     float num = (float)FMath::Sqrt(num8 + 1.0f);
     quaternion.W = num * 0.5f;
     num = 0.5f / num;
     quaternion.X = (m12 - m21) * num;
     quaternion.Y = (m20 - m02) * num;
     quaternion.Z = (m01 - m10) * num;
     return FRotator(quaternion);
   }

   if ((m00 >= m11) && (m00 >= m22))
   {
     float num7 = (float)FMath::Sqrt(((1.0f + m00) - m11) - m22);
     float num4 = 0.5f / num7;
     quaternion.X = 0.5f * num7;
     quaternion.Y = (m01 + m10) * num4;
     quaternion.Z = (m02 + m20) * num4;
     quaternion.W = (m12 - m21) * num4;
     return FRotator(quaternion);
   }

   if (m11 > m22)
   {
     float num6 = (float)FMath::Sqrt(((1.0f + m11) - m00) - m22);
     float num3 = 0.5f / num6;
     quaternion.X = (m10 + m01) * num3;
     quaternion.Y = 0.5f * num6;
     quaternion.Z = (m21 + m12) * num3;
     quaternion.W = (m20 - m02) * num3;
     return FRotator(quaternion);
   }

   float num5 = (float)FMath::Sqrt(((1.0f + m22) - m00) - m11);
   float num2 = 0.5f / num5;
   quaternion.X = (m20 + m02) * num2;
   quaternion.Y = (m21 + m12) * num2;
   quaternion.Z = 0.5f * num5;
   quaternion.W = (m01 - m10) * num2;


   return FRotator(quaternion);
}

这篇关于如何在虚幻蓝图中创建等效的Unity LookAt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆