我正在尝试为虚幻C ++中的角色设置镜头抖动。
目前,我在标头类中声明实例变量。
使用日志输出,代码将到达OnFire()函数中的正确位置,这意味着所有指针都不为空。但是相机抖动不起作用。
UPROPERTY(EditAnywhere)
UCameraShake* CShake;
//这就是我的称呼
void AZombieCharacter::BeginPlay(){
SetUpCameraShake();
}
...
void AZombieCharacter::OnFire() {
...
auto CTRLR = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (CTRLR) {
auto CM = CTRLR->PlayerCameraManager;
if (CM) {
CM->PlayCameraShake(CShake->GetClass(), 1.0f);
}
else {
UE_LOG(LogTemp, Warning, TEXT("CANNOT FIND CM"));
}
}
else {
UE_LOG(LogTemp, Warning, TEXT("CANNOT FIND CTRLR"));
}
}
...
void AZombieCharacter::SetUpCameraShake() {
CShake = UCameraShake::StaticClass()->GetDefaultObject<UCameraShake>();
CShake->OscillationDuration = 4.f;
CShake->OscillationBlendInTime = 0.5f;
CShake->OscillationBlendOutTime = 0.5f;
CShake->RotOscillation.Pitch.Amplitude = FMath::RandRange(5.0f, 10.0f);
CShake->RotOscillation.Pitch.Frequency = FMath::RandRange(25.0f, 35.0f);
CShake->RotOscillation.Yaw.Amplitude = FMath::RandRange(5.0f, 10.0f);
CShake->RotOscillation.Yaw.Frequency = FMath::RandRange(25.0f, 35.0f);
}
谢谢。
最佳答案
Play Camera Shake接受UClass
/ TSubClassOf<UCameraShake>
知道,这将使我们认为此函数最终将构造一个新对象并忽略默认对象选项。
我建议对UCameraShake
类进行子类化,并使用TSubClassOf
来存储您的类类型,而不是像CShake
这样的指针。因为可能是垃圾收集,却不知道谁知道?
这是有关UCameraShake的C ++教程。
https://unrealcpp.com/camera-shake/
未播放震动的原因可能是没有实际参数传递给震动。尽管您尝试设置默认值,但只有默认值,对于新创建的对象似乎没有考虑到它。
关于c++ - 相机晃动未激活虚幻C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57128324/