控制你角色的动画
演示效果
1.控制动画
1.1 获取动画实例
1.2 在c++中设置动画蓝图的值
auto animIns = this->GetMesh()->GetAnimInstance();
UProperty* Property = animIns->GetClass()->FindPropertyByName("XAxis");
if (Property)
{
float* ptrValue = Property->ContainerPtrToValuePtr<float>(animIns);
if (ptrValue)
{
*ptrValue = val*30;
}
}
我们的动画蓝图
1.3 获取角色前进速度,前进方向,右向
auto velocity = GetVelocity();
auto fd = GetActorForwardVector();
auto fr = GetActorRightVector();
1.4 调用c++中的基础函数库(dot)
计算速度在正向和右向上的投影
auto fp = UKismetMathLibrary::Dot_VectorVector(velocity, fv);
auto rp = UKismetMathLibrary::Dot_VectorVector(velocity, rv);
1.5 完整的移动控制代码
void ABaseRole::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
auto velocity = GetVelocity();
auto fv = GetActorForwardVector();
auto rv = GetActorRightVector();
//正向投影
auto fp = UKismetMathLibrary::Dot_VectorVector(velocity, fv);
auto rp = UKismetMathLibrary::Dot_VectorVector(velocity, rv);
auto animIns = this->GetMesh()->GetAnimInstance();
UE_LOG(LogTemp, Warning, TEXT("%f,%f"), fp, rp);
UProperty* Property = animIns->GetClass()->FindPropertyByName("YAxis");
if (Property)
{
float* ptrValue = Property->ContainerPtrToValuePtr<float>(animIns);
if (ptrValue)
{
*ptrValue = fp;
}
}
Property = animIns->GetClass()->FindPropertyByName("XAxis");
if (Property)
{
float* ptrValue = Property->ContainerPtrToValuePtr<float>(animIns);
if (ptrValue)
{
*ptrValue = rp;
}
}
}
2. 警告
如果你在这一章中出现bug不妨检测下:
BlendSpace文件中轴限值