Hi everyone, I have a spawner that is creating my actors has shown bellow
const TObjectPtr<UWorld> World = GetWorld();
if (World != nullptr)
{
FVector SpawnerLocation = ActorMesh->GetSocketLocation(*EnemySpawnLocationSocket);;
FRotator SpawnerRotation = ActorMesh->GetSocketRotation(*EnemySpawnLocationSocket);;
FActorSpawnParameters SpawnParameters;
SpawnParameters.bNoFail = true;
SpawnParameters.bDeferConstruction = false;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
FTransform EnemyTransform;
EnemyTransform.SetLocation(SpawnerLocation);
EnemyTransform.SetRotation(SpawnerRotation.Quaternion());
EnemyTransform.SetScale3D(FVector(1.0f));
TObjectPtr<ABH_EnemyBase> Enemy = GetWorld()->SpawnActorDeferred<ABH_EnemyBase>(EnemyBase, EnemyTransform);
Enemy->SetupEnemyData(EnemyData, SpawnerLocation, SpawnerRotation, DimensionLevel);
Enemy->FinishSpawning(EnemyTransform);
}
and in the SetupEnnemyData I have the following code:
void ABH_EnemyBase::SetupEnemyData(FBH_EnemyData EnemyData, FVector SpawnerLocation, FRotator SpawnerRotation, int32 CurrentDimesionLevel)
{
EnemyType = EnemyData.EnemyType;
SkeletonRelativeCapsuleLocation = EnemyData.SkeletonRelativeCapsuleLocation;
SkeletonRelativeCapsuleRotation = EnemyData.SkeletonRelativeCapsuleRotation;
SkeletonCollisionProfileName = EnemyData.SkeletonCollisionProfileName;
CapsuleHalfHeight = EnemyData.CapsuleHalfHeight;
CapsuleRadius = EnemyData.CapsuleRadius;
Health = EnemyData.Health;
HealthMultiplier = EnemyData.HealthMultiplier;
DimensionEnergy = EnemyData.DimensionEnergy;
DimensionEnergyMultiplier = EnemyData.DimensionEnergyMultiplier;
MovementSpeed = EnemyData.MovementSpeed;
AttackPowerMultiplier = EnemyData.AttackPowerMultiplier;
AttackPowerLevelMultiplier = EnemyData.AttackPowerLevelMultiplier;
AttackRange = EnemyData.AttackRange;
if (CapsuleComp != nullptr)
{
CapsuleComp->SetCapsuleRadius(CapsuleRadius);
CapsuleComp->SetCapsuleHalfHeight(CapsuleHalfHeight);
CapsuleComp->SetCollisionProfileName(SkeletonCollisionProfileName);
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
CapsuleComp->SetSimulatePhysics(true);
CapsuleComp->SetNotifyRigidBodyCollision(true);
CapsuleComp->SetUseCCD(true);
CapsuleComp->BodyInstance.bLockRotation = true;
CapsuleComp->BodyInstance.bLockXRotation = true;
CapsuleComp->BodyInstance.bLockYRotation = true;
CapsuleComp->BodyInstance.bLockZRotation = true;
CapsuleComp->BodyInstance.SetDOFLock(EDOFMode::SixDOF);
//TObjectPtr<UWorld> const World = GetWorld();
//if (World != nullptr)
//{
// FVector PlayerLocation = World->GetFirstPlayerController()->GetPawn()->GetActorLocation();
// //FRotator PlayerRotation = World->GetFirstPlayerController()->GetPawn()->GetActorRotation();
// /*FVector LinearThrow = UKismetMathLibrary::GetDirectionUnitVector(GetActorLocation(), PlayerLocation) * FMath::RandRange(300.0f, 1200.0f);
// CapsuleComp->SetPhysicsLinearVelocity(LinearThrow);*/
// FVector DirectionToPlayer = PlayerLocation - GetActorLocation();
// FRotator LaunchRotation = FRotator(0.0f, DirectionToPlayer.Rotation().Yaw, 0.0f);
///* float X = FMath::RandRange(1.0f, 2.0f);
// float Y = FMath::RandRange(1.0f, 2.0f);
// float Z = FMath::RandRange(2.0f, 5.0f);
//
// CapsuleComp->AddImpulse(FVector(0, 0, 0), NAME_None, true);*/
// //CapsuleComp->AddForce(FVector(X, Y, Z), NAME_None, false);
//}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("The Enemy Capsule Component is not created."));
}
if (SkeletonComponent != nullptr)
{
if (EnemyData.SkeletonMesh.Num() > 0)
{
int MeshToUse = FMath::RandRange(0, EnemyData.SkeletonMesh.Num() - 1);
SkeletonComponent->SetSkeletalMesh(EnemyData.SkeletonMesh[MeshToUse]);
SkeletonComponent->SetRelativeLocation(SkeletonRelativeCapsuleLocation);
SkeletonComponent->SetRelativeRotation(SkeletonRelativeCapsuleRotation);
SkeletonComponent->SetCollisionProfileName(SkeletonCollisionProfileName);
SkeletonComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
SkeletonComponent->SetNotifyRigidBodyCollision(true);
SkeletonComponent->SetUseCCD(true);
if (EnemyData.SkeletonMaterials.Num() > 0)
{
int MaterialToUse = FMath::RandRange(0, EnemyData.SkeletonMaterials.Num() - 1);
SkeletonComponent->SetMaterial(0, EnemyData.SkeletonMaterials[MaterialToUse]);
}
if (EnemyData.SkeletonAnim != nullptr)
{
SkeletonComponent->SetAnimInstanceClass(EnemyData.SkeletonAnim->GeneratedClass);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("The Enemy data doesn't have a Skeleton Animation Assigned."));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("The Enemy data doesnn't have a Skeleton Mesh Assigned."));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("The Enemy Skeleton Component is not created."));
}
EnemyLevel = CurrentDimesionLevel;
if (EnemyLevel > 1)
{
Health += Health * EnemyLevel * HealthMultiplier;
DimensionEnergy += DimensionEnergy * EnemyLevel * DimensionEnergyMultiplier;
}
if (EnemyData.EnemySpawnParticle != nullptr)
{
SpawnParticle = UNiagaraFunctionLibrary::SpawnSystemAttached(EnemyData.EnemySpawnParticle, SkeletonComponent, NAME_None, FVector::ZeroVector, FRotator::ZeroRotator, EAttachLocation::Type::SnapToTarget, true);
}
}
but for some reason even tough the AddImpulse, AddForce and the SetPhysicsLinearVelocity are all commented, the actors are spawned and still are sent flying away for some reason has shown in this video: Actors fly away.
this is the mesh that has the spawner and the socket with the location where it spawns the actors:
Spawner mesh.
Any help with this would be greatly appreciated.