void ABaseCharacter::SetRagdollPhysics() { USkeletalMeshComponent* Mesh3P = GetMesh(); if (Mesh3P) { Mesh3P->SetCollisionProfileName(TEXT("Ragdoll")); } SetActorEnableCollision(true); if (!IsPendingKill() || Mesh3P || Mesh3P->GetPhysicsAsset()) { Mesh3P->SetAllBodiesSimulatePhysics(true); Mesh3P->SetSimulatePhysics(true); Mesh3P->WakeAllRigidBodies(); Mesh3P->bBlendPhysics = true; SetLifeSpan(TimeAfterDeathBeforeDestroy); } else { // Immediately hide the pawn TurnOff(); SetActorHiddenInGame(true); SetLifeSpan(1.0f); } UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()); if (CharacterComp) { CharacterComp->StopMovementImmediately(); CharacterComp->DisableMovement(); CharacterComp->SetComponentTickEnabled(false); } }
bool ANimModCharacter::IsFalling() const { UCharacterMovementComponent* MoveComp = GetCharacterMovement(); if (MoveComp) MoveComp->IsFalling(); return false; }
bool ANimModCharacter::IsCrouched() const { UCharacterMovementComponent* MoveComp = GetCharacterMovement(); if (MoveComp) return MoveComp->IsCrouching(); return bIsCrouched; }
bool ANimModCharacter::IsMoving() const { UCharacterMovementComponent* MoveComp = GetCharacterMovement(); if (MoveComp) { FVector velocity = GetVelocity(); return MoveComp->IsMovingOnGround() && (velocity.X > 0 || velocity.Y > 0); } return false; }
//TODO: This is still an awful way to do this and we should scrap this task or do it right. void UAbilityTask_MoveToLocation::TickTask(float DeltaTime) { if (bIsFinished) { return; } Super::TickTask(DeltaTime); AActor* MyActor = GetAvatarActor(); if (MyActor) { ACharacter* MyCharacter = Cast<ACharacter>(MyActor); if (MyCharacter) { UCharacterMovementComponent* CharMoveComp = Cast<UCharacterMovementComponent>(MyCharacter->GetMovementComponent()); if (CharMoveComp) { CharMoveComp->SetMovementMode(MOVE_Custom, 0); } } float CurrentTime = GetWorld()->GetTimeSeconds(); if (CurrentTime >= TimeMoveWillEnd) { bIsFinished = true; // Teleport in attempt to find a valid collision spot MyActor->TeleportTo(TargetLocation, MyActor->GetActorRotation()); if (!bIsSimulating) { MyActor->ForceNetUpdate(); OnTargetLocationReached.Broadcast(); EndTask(); } } else { float MoveFraction = (CurrentTime - TimeMoveStarted) / DurationOfMovement; if (LerpCurve) { MoveFraction = LerpCurve->GetFloatValue(MoveFraction); } MyActor->SetActorLocation(FMath::Lerp<FVector, float>(StartLocation, TargetLocation, MoveFraction)); } } else { bIsFinished = true; EndTask(); } }
// Sets default values ASCharacter::ASCharacter(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; UCharacterMovementComponent* MoveComp = GetCharacterMovement(); // Adjust jump to make it less floaty MoveComp->GravityScale = 1.5f; MoveComp->JumpZVelocity = 620; MoveComp->bCanWalkOffLedgesWhenCrouching = true; MoveComp->MaxWalkSpeedCrouched = 200; /* Ignore this channel or it will absorb the trace impacts instead of the skeletal mesh */ GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Ignore); // Enable crouching MoveComp->GetNavAgentPropertiesRef().bCanCrouch = true; CameraBoomComp = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom")); CameraBoomComp->SocketOffset = FVector(0, 35, 0); CameraBoomComp->TargetOffset = FVector(0, 0, 55); CameraBoomComp->bUsePawnControlRotation = true; CameraBoomComp->AttachParent = GetRootComponent(); CameraComp = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Camera")); CameraComp->AttachParent = CameraBoomComp; CarriedObjectComp = ObjectInitializer.CreateDefaultSubobject<USCarryObjectComponent>(this, TEXT("CarriedObjectComp")); CarriedObjectComp->AttachParent = GetRootComponent(); MaxUseDistance = 500; DropWeaponMaxDistance = 100; bHasNewFocus = true; TargetingSpeedModifier = 0.5f; SprintingSpeedModifier = 2.5f; Health = 100; IncrementHungerAmount = 5.0f; IncrementHungerInterval = 5.0f; CriticalHungerThreshold = 90; HungerDamagePerInterval = 1.0f; MaxHunger = 100; Hunger = 0; /* Names as specified in the character skeleton */ WeaponAttachPoint = TEXT("WeaponSocket"); PelvisAttachPoint = TEXT("PelvisSocket"); SpineAttachPoint = TEXT("SpineSocket"); }
void AGameModeBase::SetPlayerDefaults(APawn* PlayerPawn) { PlayerPawn->SetPlayerDefaults(); #if !UE_WITH_PHYSICS // If there is no physics, set to flying by default UCharacterMovementComponent* CharacterMovement = Cast<UCharacterMovementComponent>(NewPlayer->GetPawn()->GetMovementComponent()); if (CharacterMovement) { CharacterMovement->bCheatFlying = true; CharacterMovement->SetMovementMode(MOVE_Flying); } #endif //!UE_WITH_PHYSICS }
void UAbilityTask_MoveToLocation::OnDestroy(bool AbilityIsEnding) { AActor* MyActor = GetAvatarActor(); if (MyActor) { ACharacter* MyCharacter = Cast<ACharacter>(MyActor); if (MyCharacter) { UCharacterMovementComponent* CharMoveComp = Cast<UCharacterMovementComponent>(MyCharacter->GetMovementComponent()); if (CharMoveComp && CharMoveComp->MovementMode == MOVE_Custom) { CharMoveComp->SetMovementMode(MOVE_Falling); } } } Super::OnDestroy(AbilityIsEnding); }
void AShooterCharacter::SetRagdollPhysics() { bool bInRagdoll = false; USkeletalMeshComponent* Mesh3P = GetMesh(); if (IsPendingKill()) { bInRagdoll = false; } else if (!Mesh3P || !Mesh3P->GetPhysicsAsset()) { bInRagdoll = false; } else { Mesh3P->SetAllBodiesSimulatePhysics(true); Mesh3P->SetSimulatePhysics(true); Mesh3P->WakeAllRigidBodies(); Mesh3P->bBlendPhysics = true; bInRagdoll = true; } UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()); if (CharacterComp) { CharacterComp->StopMovementImmediately(); CharacterComp->DisableMovement(); CharacterComp->SetComponentTickEnabled(false); } if (!bInRagdoll) { // Immediately hide the pawn TurnOff(); SetActorHiddenInGame(true); SetLifeSpan(1.0f); } else { SetLifeSpan(10.0f); } }
ANimModCharacter::ANimModCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { // Set size for collision capsule GetCapsuleComponent()->InitCapsuleSize(46.f, 92.0f); /*CharacterCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera")); CharacterCameraComponent->AttachParent = GetCapsuleComponent();*/ DefaultBaseEyeHeight = 71.f; BaseEyeHeight = DefaultBaseEyeHeight; //CharacterCameraComponent->RelativeLocation = FVector(0, 0, DefaultBaseEyeHeight); // Position the camera //CharacterCameraComponent->bUsePawnControlRotation = true; Mesh1P = ObjectInitializer.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("PawnMesh1P")); Mesh1P->AttachParent = GetCapsuleComponent(); Mesh1P->SetOnlyOwnerSee(true); /*Mesh1P->bOwnerNoSee = false;*/ Mesh1P->bCastDynamicShadow = false; Mesh1P->bReceivesDecals = false; Mesh1P->MeshComponentUpdateFlag = EMeshComponentUpdateFlag::OnlyTickPoseWhenRendered; Mesh1P->PrimaryComponentTick.TickGroup = TG_PrePhysics; Mesh1P->bChartDistanceFactor = false; Mesh1P->SetCollisionObjectType(ECC_Pawn); Mesh1P->SetCollisionEnabled(ECollisionEnabled::NoCollision); Mesh1P->SetCollisionResponseToAllChannels(ECR_Ignore); /*GetMesh()->bOnlyOwnerSee = false; GetMesh()->bOwnerNoSee = true;*/ GetMesh()->SetOnlyOwnerSee(false); GetMesh()->SetOwnerNoSee(true); GetMesh()->bReceivesDecals = false; GetMesh()->SetCollisionObjectType(ECC_Pawn); GetMesh()->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); GetMesh()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Block); GetMesh()->SetCollisionResponseToChannel(COLLISION_PROJECTILE, ECR_Block); GetMesh()->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block); GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore); GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_PROJECTILE, ECR_Block); GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Ignore); UCharacterMovementComponent* MoveComp = GetCharacterMovement(); // Adjust jump to make it less floaty MoveComp->GravityScale = 1.5f; MoveComp->JumpZVelocity = 620; MoveComp->bCanWalkOffLedgesWhenCrouching = true; MoveComp->MaxWalkSpeedCrouched = 200; MoveComp->AirControl = 1; /* Ignore this channel or it will absorb the trace impacts instead of the skeletal mesh */ //GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Ignore); // Enable crouching MoveComp->GetNavAgentPropertiesRef().bCanCrouch = true; TargetingSpeedModifier = 1.5f; bIsTargeting = false; RunningSpeedModifier = 5.0f; bWantsToRun = false; bWantsToFire = false; LowHealthPercentage = 0.5f; BaseTurnRate = 45.f; BaseLookUpRate = 45.f; }
void ANimModCharacter::OnUnCrouch() { UCharacterMovementComponent* MoveComp = GetCharacterMovement(); if (MoveComp && MoveComp->IsCrouching()) UnCrouch(); }
void FGameplayDebuggerCategory_AI::CollectData(APlayerController* OwnerPC, AActor* DebugActor) { APawn* MyPawn = Cast<APawn>(DebugActor); ACharacter* MyChar = Cast<ACharacter>(MyPawn); DataPack.PawnName = MyPawn ? MyPawn->GetHumanReadableName() : FString(TEXT("{red}No selected pawn.")); DataPack.bIsUsingCharacter = (MyChar != nullptr); AAIController* MyController = MyPawn ? Cast<AAIController>(MyPawn->Controller) : nullptr; DataPack.bHasController = (MyController != nullptr); if (MyController) { if (MyController->IsPendingKill() == false) { DataPack.ControllerName = MyController->GetName(); } else { DataPack.ControllerName = TEXT("Controller PENDING KILL"); } } else { DataPack.ControllerName = TEXT("No Controller"); } if (MyPawn && !MyPawn->IsPendingKill()) { UCharacterMovementComponent* CharMovementComp = MyChar ? MyChar->GetCharacterMovement() : nullptr; if (CharMovementComp) { UPrimitiveComponent* FloorComponent = MyPawn->GetMovementBase(); AActor* FloorActor = FloorComponent ? FloorComponent->GetOwner() : nullptr; DataPack.MovementBaseInfo = FloorComponent ? FString::Printf(TEXT("%s.%s"), *GetNameSafe(FloorActor), *FloorComponent->GetName()) : FString(TEXT("None")); DataPack.MovementModeInfo = CharMovementComp->GetMovementName(); } UBehaviorTreeComponent* BehaviorComp = MyController ? Cast<UBehaviorTreeComponent>(MyController->BrainComponent) : nullptr; DataPack.bIsUsingBehaviorTree = (BehaviorComp != nullptr); if (BehaviorComp) { DataPack.CurrentAITask = BehaviorComp->DescribeActiveTasks(); DataPack.CurrentAIState = BehaviorComp->IsRunning() ? TEXT("Running") : BehaviorComp->IsPaused() ? TEXT("Paused") : TEXT("Inactive"); DataPack.CurrentAIAssets = BehaviorComp->DescribeActiveTrees(); } UGameplayTasksComponent* TasksComponent = MyController ? MyController->GetGameplayTasksComponent() : nullptr; DataPack.bIsUsingGameplayTasks = (TasksComponent != nullptr); if (TasksComponent) { for (FConstGameplayTaskIterator It = TasksComponent->GetTickingTaskIterator(); It; ++It) { const UGameplayTask* TaskOb = *It; if (TaskOb) { DataPack.TickingTaskInfo += DescribeTaskHelper(*TaskOb); DataPack.NumTickingTasks++; } } for (FConstGameplayTaskIterator It = TasksComponent->GetPriorityQueueIterator(); It; ++It) { const UGameplayTask* TaskOb = *It; if (TaskOb) { DataPack.TaskQueueInfo += DescribeTaskHelper(*TaskOb); DataPack.NumTasksInQueue++; } } } DataPack.MontageInfo = MyChar ? GetNameSafe(MyChar->GetCurrentMontage()) : FString(); UNavigationSystem* NavSys = UNavigationSystem::GetCurrent(MyPawn->GetWorld()); const ANavigationData* NavData = MyController && NavSys ? NavSys->GetNavDataForProps(MyController->GetNavAgentPropertiesRef()) : nullptr; DataPack.NavDataInfo = NavData ? NavData->GetConfig().Name.ToString() : FString(); CollectPathData(MyController); } else { PathDataPack.PathCorridor.Reset(); PathDataPack.PathPoints.Reset(); } }