UE4 设置旋转值到对象,使某个 actor 始终对着它指向 目标

值作思路:

    1.假设 A 物体需要指向 B 物体的方向

    2.第一步: 需要获取两个物体的位置, 可通过 GetActorLocation() 方法获取 

    3.第二步: 需要获取两个物体的旋转值偏移量,可通过UE引擎自带的 FindLookAtRotation 函数获取

    4.第三步: 获取旋转插值, 可通过RInterpTo函数实现 用RInterpTo 函数主要是为了使动画不僵硬,也可直接通过第四步完成设置

    5.第四步: 最后通过设置A的旋转值,使A的旋转值指向B

蓝图连接方法,请到下方链接中的网站里查看(该网站可直接复制蓝图):

http://blueprintue.cn/blueprint/dtkxu1id/

代码如下:

1.

//1.这段代码在tick中运行,也可以自行写定时器
//设置旋转
		FRotator LookAtYaw = GetLookAtRotationYaw(CombatTraget->GetActorLocation());
		//设置旋转过渡,使动画不僵硬
		FRotator InterpRotation = FMath::RInterpTo(GetActorRotation(), LookAtYaw,     DeltaTime, InterpSpeed);

		SetActorRotation(InterpRotation);

2.获取旋转偏移量代码如下:

FRotator AMain::GetLookAtRotationYaw(FVector Target) {
	//获取两个目标之间的偏移量
	FRotator LookAtRotation = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), Target);
	FRotator LookAtRotationYaw(0.f,LookAtRotation.Yaw,0.f);
	return LookAtRotationYaw;
}

我是UE4新人,如果有错漏的地方,还请多多见谅

猜你喜欢

转载自blog.csdn.net/qq_34970171/article/details/115153151