void APlayerOvi::CalculateOrientation(){ FVector forward = GetActorForwardVector(); FVector up = GetActorUpVector(); float dotForward = FVector::DotProduct(GetActorLocation(), forward); if (dotForward > m_limit && m_state == States::RIGHT) Rotate(FVector(0, 0, -90)); if (dotForward > m_limit && m_state == States::LEFT) Rotate(FVector(0, 0, 90)); float dotUp = FVector::DotProduct(GetActorLocation(), up); float val = 0; if (dotUp > m_limit || dotUp < -m_limit) { bool toUp = dotUp > m_limit; val = (toUp) ? 90 : -90; if (m_state == States::RIGHT) Rotate(FVector(-val, 0, 0)); else if (m_state == States::LEFT) Rotate(FVector(val, 0, 0)); } AjustPosition(); }
void APlayerOvi::CalculateGravity(float DeltaSeconds){ FVector up = GetActorUpVector(); FVector location = GetActorLocation(); if (m_enabledGravity && !m_hasLanded && !m_isOnMobilePlatform) { if (m_actualJumpSpeed < FallSpeed) m_actualJumpSpeed += AccelerationFall * DeltaSeconds; else{ m_actualJumpSpeed = FallSpeed; } location -= m_actualJumpSpeed * DeltaSeconds * up; SetActorLocation(location); } }
void AUnsealed4x4Pawn::UpdatePhysicsMaterial() { if (GetActorUpVector().Z < 0) { if (bIsLowFriction == true) { GetMesh()->SetPhysMaterialOverride(NonSlipperyMaterial); bIsLowFriction = false; } else { GetMesh()->SetPhysMaterialOverride(SlipperyMaterial); bIsLowFriction = true; } } }
void AMobileEnemy::CalculateGravity(float DeltaSeconds){ FVector up = GetActorUpVector(); FVector location = m_nextPosition; if (m_enableGravity) { if (m_actualJumpSpeed < m_jumpSpeed) m_actualJumpSpeed += m_accelerationJump * DeltaSeconds; else{ m_actualJumpSpeed = m_jumpSpeed; } location -= m_actualJumpSpeed * DeltaSeconds * up; m_nextPosition = location; CheckCollision(); } else { m_actualJumpSpeed = 0; } }
void AExplosiveMine::explode() { if (stepOffSound) UGameplayStatics::PlaySoundAtLocation(this, stepOffSound, GetActorLocation()); if (explosiveTemplate) { AGrenade* gren = GetWorld()->SpawnActor<AGrenade>(explosiveTemplate, GetActorLocation() + GetActorUpVector() * 10, GetActorUpVector().ToOrientationRotator()); if (gren) { gren->ProjectileMovement->MaxSpeed = 100; gren->ProjectileMovement->InitialSpeed = 100; gren->ProjectileMovement->Velocity = (GetActorUpVector() * 10); gren->activate(); } } isAboutToExplode = true; Destroy(); }
bool ADraggableMoveTile::cameraRayIntersectWithTilePlane( const FVector& camlocation, const FVector& dir, FVector& hitPoint) { bool hitPlane = false; auto planeNormal = GetActorUpVector(); auto demon = FMath::Abs(FVector::DotProduct(dir, planeNormal)); if (demon > FLT_EPSILON) { auto t = FVector::DotProduct(camlocation - GetActorLocation(), planeNormal) / demon; hitPlane = t >= 0; if (hitPlane) { hitPoint = camlocation + dir * t; } } return hitPlane; }
void APlayerOvi::DoJump(float DeltaSeconds){ if (m_hasLanded && !m_doJump) { m_isJumping = false; m_headCollision = false; } if (m_doJump && !m_isJumping && m_hasLanded) { m_hasLanded = false; m_isJumping = true; m_headCollision = false; m_doJump = false; } FVector up = GetActorUpVector(); FVector location = GetActorLocation(); if (m_isJumping && !m_headCollision) { if (m_actualJumpSpeed > 0) { location += m_actualJumpSpeed * DeltaSeconds * up; m_actualJumpSpeed -= AccelerationJump * DeltaSeconds; if (m_actualJumpSpeed < 0) m_actualJumpSpeed = 0; m_enabledGravity = false; m_isFalling = false; } else { m_actualJumpSpeed = 0; m_isJumping = false; } } else { m_isFalling = !m_hasLanded; m_enabledGravity = true; } SetActorLocation(location); }
//------------------------------------------------------------------------------------- //Creates a closed area using the character's position as its center and enables //the bouncing effect on all the projectiles for the lifespan of the area. //------------------------------------------------------------------------------------- void ARnCCharacter::CreateProjectileBounceArea() { if(BounceAreaBP) { //The spawn params that are used in all spawns FActorSpawnParameters SpawnParams; DECLARE_DELEGATE_TwoParams(BounceAreaSpawner, FVector, FRotator); BounceAreaSpawner Delegate; //Spawns a wall based on the relative location and rotation given Delegate.BindLambda([&](FVector WallRelativeLocation, FRotator WallRelativeRotation) { //Take into consideration the location of the player in order to spawn the walls around him WallRelativeLocation += GetActorLocation(); WallRelativeRotation += GetActorRotation(); //Create the desired transform FTransform WallTransform; WallTransform.SetLocation(WallRelativeLocation); WallTransform.SetRotation(WallRelativeRotation.Quaternion()); //Spawn the wall and apply the desired scale AProjectileBounceArea* BounceArea = GetWorld()->SpawnActor<AProjectileBounceArea>(BounceAreaBP, WallTransform, SpawnParams); if (BounceArea) { BounceArea->SetActorScale3D(UnifiedScaleMultiplier); BounceArea->SetLifeSpan(LifeTimeOfBounceArea); } }); if(Delegate.IsBound()) { //Enable the battle cry animation if (MainCharAnimInstance) { bIsInBattleCry = true; MainCharAnimInstance->EnableBattlecry(); } //Enabling projectile bouncing effect bShouldProjectilesBounce = true; //Disable the projectiles bouncing after a certain amount of time FTimerHandle TimerHandle; FTimerDelegate TimerDelegate; TimerDelegate.BindLambda([&]() { bShouldProjectilesBounce = false; }); GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, LifeTimeOfBounceArea, false); //We're going to use the following vectors in order to spawn the walls in the desired rotation and location FVector ForwardActorVector = GetActorForwardVector(); FVector RightActorVector = GetActorRightVector(); //Spawning the wall that the player will face Delegate.Execute(ForwardActorVector * ForwardVectorMultiplier, FRotator::ZeroRotator); //Spawning the wall right behind the player Delegate.Execute((ForwardActorVector * -1) * ForwardVectorMultiplier, FRotator::ZeroRotator); //Spawning the wall on the right side of the player Delegate.Execute(RightActorVector * RightVectorMultiplier, FRotator(0, 90.f, 0)); //Spawning the wall on the left side of the player Delegate.Execute((RightActorVector * -1) * RightVectorMultiplier, FRotator(0, 90.f, 0)); //Spawning the "ceiling" wall Delegate.Execute(GetActorUpVector() * UpVectorMultiplier, FRotator(90.f,0,0)); } } }
void AMobileEnemy::CheckCollision() { //Arrays RayCasting Results TArray<FHitResult> OutTraceResultMiddle; TArray<FHitResult> OutTraceResultTop; TArray<FHitResult> OutTraceResultBottom; //RayCasting StartPoint FVector StartTrace = m_lastPosition; FVector StartTraceTop = StartTrace + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBottom = StartTrace - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); //Calculate de horizontal ray, to avoid "Y-AXIS Affect" FVector newLocationForward; newLocationForward.X = (FMath::Abs(GetActorRightVector().X) <= 0.01) ? m_lastPosition.X : m_nextPosition.X; newLocationForward.Y = (FMath::Abs(GetActorRightVector().Y) <= 0.01) ? m_lastPosition.Y : m_nextPosition.Y; newLocationForward.Z = (FMath::Abs(GetActorRightVector().Z) <= 0.01) ? m_lastPosition.Z : m_nextPosition.Z; //RayCasting EndPoint const FVector EndTraceMidle = newLocationForward + GetActorRightVector() * m_capsuleRadious; const FVector EndTraceTop = (newLocationForward + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) + GetActorRightVector() * m_capsuleRadious; const FVector EndTraceBottom = (newLocationForward - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) + GetActorRightVector() * m_capsuleRadious; // Setup the trace query static FName FireTraceIdent = FName(TEXT("Platform")); FCollisionQueryParams TraceParams(FireTraceIdent, true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bFindInitialOverlaps = true; TraceParams.bTraceComplex = true; FCollisionResponseParams ResponseParam(ECollisionResponse::ECR_Overlap); //FORWARD RAYCASTING GetWorld()->LineTraceMulti(OutTraceResultMiddle, StartTrace, EndTraceMidle, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionMidle = OutTraceResultMiddle.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceTop, EndTraceTop, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultTop, StartTraceTop, EndTraceTop, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionTop = OutTraceResultTop.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultBottom, StartTraceBottom, EndTraceBottom, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionBottom = OutTraceResultBottom.Num() > 0; //check midle raycast if (collisionMidle) { int size = OutTraceResultMiddle.Num(); for (int i = 0; i < size; i++) if (OutTraceResultMiddle[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultMiddle[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultMiddle[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultMiddle[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } //check top raycast if (collisionTop) { int size = OutTraceResultTop.Num(); for (int i = 0; i < size; i++) if (OutTraceResultTop[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultTop[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultTop[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultTop[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } //check bottom raycast if (collisionBottom) { int size = OutTraceResultBottom.Num(); for (int i = 0; i < size; i++) if (OutTraceResultBottom[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultBottom[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultBottom[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(GetActorRightVector(), m_nextPosition, OutTraceResultBottom[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } //BACKWARD RAYCASTING TArray<FHitResult> OutTraceResultTopBack; TArray<FHitResult> OutTraceResultMiddleBack; TArray<FHitResult> OutTraceResultBottomBack; newLocationForward.X = (FMath::Abs(GetActorRightVector().X) <= 0.01) ? m_lastPosition.X : m_nextPosition.X; newLocationForward.Y = (FMath::Abs(GetActorRightVector().Y) <= 0.01) ? m_lastPosition.Y : m_nextPosition.Y; newLocationForward.Z = (FMath::Abs(GetActorRightVector().Z) <= 0.01) ? m_lastPosition.Z : m_nextPosition.Z; // Calculate the start location for trace back FVector StartTraceBack = newLocationForward; FVector StartTraceTopBack = StartTraceBack + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBottomBack = StartTraceBack - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); const FVector EndTraceTopBack = (m_lastPosition + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) - GetActorRightVector() * (m_capsuleRadious - 5); const FVector EndTraceBottomBack = (m_lastPosition - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) - GetActorRightVector() * (m_capsuleRadious - 5); const FVector EndTraceMidleBack = m_lastPosition - GetActorRightVector() * (m_capsuleRadious - 5); GetWorld()->LineTraceMulti(OutTraceResultTopBack, StartTraceTopBack, EndTraceTopBack, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionTopBack = OutTraceResultTopBack.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultBottomBack, StartTraceBottomBack, EndTraceBottomBack, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionBottomBack = OutTraceResultBottomBack.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultMiddleBack, StartTraceBack, EndTraceMidleBack, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionMidleBack = OutTraceResultMiddleBack.Num() > 0; //check midle raycast if (collisionMidleBack) { int size = OutTraceResultMiddleBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultMiddleBack[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultMiddleBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultMiddleBack[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultMiddleBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } //check top raycast if (collisionTopBack) { int size = OutTraceResultTopBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultTopBack[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultTopBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultTopBack[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultTopBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } //check bottom raycast if (collisionBottomBack) { int size = OutTraceResultBottomBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultBottomBack[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultBottomBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } else if (OutTraceResultBottomBack[i].GetActor()->ActorHasTag("Player")) { if (m_tickCounter > 5) { ATowardsTheLightGameMode *gameMode = Cast<ATowardsTheLightGameMode>(UGameplayStatics::GetGameMode(this)); if (gameMode) if (gameMode->EndGameBP() > -0.05) gameMode->EndGame(ATowardsTheLightGameMode::DEFEAT); if (!Fly) m_nextPosition = RecalculateLocation(-GetActorRightVector(), m_nextPosition, OutTraceResultBottomBack[i].Location, m_capsuleRadious); ResponseCollision(); break; } } } if (!Fly) { TArray<FHitResult> OutTraceResultDown; TArray<FHitResult> OutTraceResultDownLeftF; TArray<FHitResult> OutTraceResultDownRigthF; FVector StartTraceLeftF = StartTrace + GetActorRightVector() * (m_capsuleRadious - m_capsuleRadiousPadding); FVector StartTraceRigthF = StartTrace - GetActorRightVector() * (m_capsuleRadious - m_capsuleRadiousPadding); FVector newLocationUp; newLocationUp.X = (FMath::Abs(GetActorUpVector().X) <= 0.01) ? m_lastPosition.X : m_nextPosition.X; newLocationUp.Y = (FMath::Abs(GetActorUpVector().Y) <= 0.01) ? m_lastPosition.Y : m_nextPosition.Y; newLocationUp.Z = (FMath::Abs(GetActorUpVector().Z) <= 0.01) ? m_lastPosition.Z : m_nextPosition.Z; const FVector EndTraceDown = newLocationUp - GetActorUpVector() * m_capsuleHeight; const FVector EndTraceDownLeftF = (newLocationUp + GetActorRightVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) - GetActorUpVector() * m_capsuleHeight; const FVector EndTraceDownRightF = (newLocationUp - GetActorRightVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) - GetActorUpVector() * m_capsuleHeight; GetWorld()->LineTraceMulti(OutTraceResultDownLeftF, StartTraceLeftF, EndTraceDownLeftF, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionDownLeftF = OutTraceResultDownLeftF.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultDownRigthF, StartTraceRigthF, EndTraceDownRightF, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionDownRightF = OutTraceResultDownRigthF.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultDown, StartTrace, EndTraceDown, COLLISION_ENEMY, TraceParams, ResponseParam); bool collisionDown = OutTraceResultDown.Num() > 0; bool eGravity = true; if (collisionDown || collisionDownLeftF || collisionDownRightF) { if (collisionDown) { int size = OutTraceResultDown.Num(); for (int i = 0; i < size; i++) if (OutTraceResultDown[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorUpVector(), m_nextPosition, OutTraceResultDown[i].Location, -m_capsuleHeight); m_enableGravity = false; eGravity = false; m_actualJumpSpeed = 0; break; } } else if (collisionDownLeftF) { int size = OutTraceResultDownLeftF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultDownLeftF[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorUpVector(), m_nextPosition, OutTraceResultDownLeftF[i].Location, -m_capsuleHeight); m_enableGravity = false; eGravity = false; m_actualJumpSpeed = 0; break; } } else if (collisionDownRightF) { int size = OutTraceResultDownRigthF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultDownRigthF[i].GetActor()->ActorHasTag("Platform")) { m_nextPosition = RecalculateLocation(GetActorUpVector(), m_nextPosition, OutTraceResultDownRigthF[i].Location, -m_capsuleHeight); m_enableGravity = false; eGravity = false; m_actualJumpSpeed = 0; break; } } else { m_enableGravity = true; } if (eGravity) m_enableGravity = true; } else { m_enableGravity = true; } } }
void APlayerOvi::CheckCollision() { TArray<FHitResult> OutTraceResultTop; TArray<FHitResult> OutTraceResultBody; TArray<FHitResult> OutTraceResultMiddle; TArray<FHitResult> OutTraceResultLegs; TArray<FHitResult> OutTraceResultBottom; // Calculate the start location for trace FVector StartTrace = m_lastPosition; FVector StartTraceTop = StartTrace + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBottom = StartTrace - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBody = StartTrace + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet); FVector StartTraceLegs = StartTrace - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet); FVector StartTraceLeftF = StartTrace + GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding); FVector StartTraceRigthF = StartTrace - GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding); // Calculate the end location for trace //horizontal FVector newLocationForward; newLocationForward.X = (FMath::Abs(GetActorForwardVector().X) <= 0.01) ? m_lastPosition.X : GetActorLocation().X; newLocationForward.Y = (FMath::Abs(GetActorForwardVector().Y) <= 0.01) ? m_lastPosition.Y : GetActorLocation().Y; newLocationForward.Z = (FMath::Abs(GetActorForwardVector().Z) <= 0.01) ? m_lastPosition.Z : GetActorLocation().Z; const FVector EndTraceTop = (newLocationForward + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) + GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceBottom = (newLocationForward - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) + GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceBody = (newLocationForward + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet)) + GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceLegs = (newLocationForward - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet)) + GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceMidle = newLocationForward + GetActorForwardVector() * m_capsuleRadious; FCollisionResponseParams ResponseParam(ECollisionResponse::ECR_Overlap); GetWorld()->LineTraceMulti(OutTraceResultTop, StartTraceTop, EndTraceTop, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionTop = OutTraceResultTop.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceTop, EndTraceTop, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultBottom, StartTraceBottom, EndTraceBottom, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionBottom = OutTraceResultBottom.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceBottom, EndTraceBottom, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultBody, StartTraceBody, EndTraceBody, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionBody = OutTraceResultBody.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceBody, EndTraceBody, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultLegs, StartTraceLegs, EndTraceLegs, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionLegs = OutTraceResultLegs.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceLegs, EndTraceLegs, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultMiddle, StartTrace, EndTraceMidle, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionMidle = OutTraceResultMiddle.Num() > 0; //DrawDebugLine(GetWorld(), StartTrace, EndTraceMidle, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); if (collisionTop || collisionBottom || collisionMidle) { if (collisionMidle) { int size = OutTraceResultMiddle.Num(); for (int i = 0; i < size; i++) if (OutTraceResultMiddle[i].GetActor()->ActorHasTag("Platform")) { SetActorLocation(RecalculateLocation(GetActorForwardVector(), GetActorLocation(), OutTraceResultMiddle[i].Location, m_capsuleRadious)); break; } } if (collisionTop) { int size = OutTraceResultTop.Num(); for (int i = 0; i < size; i++) if (OutTraceResultTop[i].GetActor()->ActorHasTag("Platform")) { SetActorLocation(RecalculateLocation(GetActorForwardVector(), GetActorLocation(), OutTraceResultTop[i].Location, m_capsuleRadious)); break; } } if (collisionBottom) { int size = OutTraceResultBottom.Num(); for (int i = 0; i < size; i++) if (OutTraceResultBottom[i].GetActor()->ActorHasTag("Platform")) { SetActorLocation(RecalculateLocation(GetActorForwardVector(), GetActorLocation(), OutTraceResultBottom[i].Location, m_capsuleRadious)); break; } } } else { TArray<FHitResult> OutTraceResultTopBack; TArray<FHitResult> OutTraceResultBodyBack; TArray<FHitResult> OutTraceResultMiddleBack; TArray<FHitResult> OutTraceResultBottomBack; TArray<FHitResult> OutTraceResultLegsBack; // Calculate the start location for trace back FVector StartTraceBack = newLocationForward; FVector StartTraceTopBack = StartTraceBack + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBottomBack = StartTraceBack - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding); FVector StartTraceBodyBack = StartTraceBack + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet); FVector StartTraceLegsBack = StartTraceBack - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet); //cambiado a last_position porque va hacia atras y la posicion nueva siempre sera hacia adelante. const FVector EndTraceTopBack = (m_lastPosition + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) - GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceBottomBack = (m_lastPosition - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPadding)) - GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceBodyBack = (m_lastPosition + GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet)) - GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceLegsBack = (m_lastPosition - GetActorUpVector() * (m_capsuleHeight - m_capsuleHeightPaddingFeet)) - GetActorForwardVector() * m_capsuleRadious; const FVector EndTraceMidleBack = m_lastPosition - GetActorForwardVector() * m_capsuleRadious; GetWorld()->LineTraceMulti(OutTraceResultTopBack, StartTraceTopBack, EndTraceTopBack, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionTopBack = OutTraceResultTopBack.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceTopBack, EndTraceTopBack, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultBottomBack, StartTraceBottomBack, EndTraceBottomBack, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionBottomBack = OutTraceResultBottomBack.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceBottomBack, EndTraceBottomBack, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultBodyBack, StartTraceBodyBack, EndTraceBodyBack, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionBodyBack = OutTraceResultBodyBack.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceBodyBack, EndTraceBodyBack, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultLegsBack, StartTraceLegsBack, EndTraceLegsBack, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionLegsBack = OutTraceResultLegsBack.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceLegsBack, EndTraceLegsBack, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); GetWorld()->LineTraceMulti(OutTraceResultMiddleBack, StartTraceBack, EndTraceMidleBack, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionMidleBack = OutTraceResultMiddleBack.Num() > 0; //DrawDebugLine(GetWorld(), StartTraceBack, EndTraceMidleBack, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); if (collisionTopBack || collisionBottomBack || collisionMidleBack) { if (collisionMidleBack) { int size = OutTraceResultMiddleBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultMiddleBack[i].GetActor()->ActorHasTag("MobilePlatform")) { if (!isPlayerRunning()) SetActorLocation(RecalculateLocation(-GetActorForwardVector(), GetActorLocation(), OutTraceResultMiddleBack[i].Location, m_capsuleRadious)); break; } } if (collisionTopBack) { if (collisionBodyBack) { int size = OutTraceResultTopBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultTopBack[i].GetActor()->ActorHasTag("MobilePlatform")) { if (!isPlayerRunning()) SetActorLocation(RecalculateLocation(-GetActorForwardVector(), GetActorLocation(), OutTraceResultTopBack[i].Location, m_capsuleRadious)); break; } } } if (collisionBottomBack) { if (collisionLegsBack) { int size = OutTraceResultBottomBack.Num(); for (int i = 0; i < size; i++) if (OutTraceResultBottomBack[i].GetActor()->ActorHasTag("MobilePlatform")) { if (!isPlayerRunning()) SetActorLocation(RecalculateLocation(-GetActorForwardVector(), GetActorLocation(), OutTraceResultBottomBack[i].Location, m_capsuleRadious)); break; } } } } } // Calculate the end location for trace // Vertical TArray<FHitResult> OutTraceResultDown; TArray<FHitResult> OutTraceResultDownLeftF; TArray<FHitResult> OutTraceResultDownRigthF; TArray<FHitResult> OutTraceResultUp; TArray<FHitResult> OutTraceResultUpLeftF; TArray<FHitResult> OutTraceResultUpRigthF; FVector newLocationUp; newLocationUp.X = (FMath::Abs(GetActorUpVector().X) <= 0.01) ? m_lastPosition.X : GetActorLocation().X; newLocationUp.Y = (FMath::Abs(GetActorUpVector().Y) <= 0.01) ? m_lastPosition.Y : GetActorLocation().Y; newLocationUp.Z = (FMath::Abs(GetActorUpVector().Z) <= 0.01) ? m_lastPosition.Z : GetActorLocation().Z; bool action = false; const FVector EndTraceDownLeftF = (newLocationUp + GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) - GetActorUpVector() * m_capsuleHeight; GetWorld()->LineTraceMulti(OutTraceResultDownLeftF, StartTraceLeftF, EndTraceDownLeftF, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionDownLeftF = OutTraceResultDownLeftF.Num() > 0; if (collisionDownLeftF) { if (collisionBottom == collisionLegs) { int size = OutTraceResultDownLeftF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultDownLeftF[i].GetActor()->ActorHasTag("Platform")) { if (OutTraceResultDownLeftF[i].GetActor()->ActorHasTag("MobilePlatform")) { AMobilePlatform *movil = dynamic_cast<AMobilePlatform *>(OutTraceResultDownLeftF[i].GetActor()); if (movil) { if (m_currentMobile) if (m_currentMobile != movil) m_currentMobile->SetPlayerOn(false); m_currentMobile = movil; m_currentMobile->SetPlayerOn(true); } } else { if (m_currentMobile) { m_currentMobile->SetPlayerOn(false); m_isOnMobilePlatform = false; m_currentMobile = nullptr; } } FVector localizaion = OutTraceResultDownLeftF[i].Location; action = true; SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultDownLeftF[i].Location, -m_capsuleHeight)); m_hasLanded = true; m_isFalling = false; m_actualJumpSpeed = JumpSpeed; break; } } } newLocationUp.X = (FMath::Abs(GetActorUpVector().X) <= 0.01) ? m_lastPosition.X : GetActorLocation().X; newLocationUp.Y = (FMath::Abs(GetActorUpVector().Y) <= 0.01) ? m_lastPosition.Y : GetActorLocation().Y; newLocationUp.Z = (FMath::Abs(GetActorUpVector().Z) <= 0.01) ? m_lastPosition.Z : GetActorLocation().Z; const FVector EndTraceDownRightF = (newLocationUp - GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) - GetActorUpVector() * m_capsuleHeight; GetWorld()->LineTraceMulti(OutTraceResultDownRigthF, StartTraceRigthF, EndTraceDownRightF, COLLISION_PLAYER, m_TraceParams, ResponseParam); bool collisionDownRightF = OutTraceResultDownRigthF.Num() > 0; if (collisionDownRightF) { int size = OutTraceResultDownRigthF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultDownRigthF[i].GetActor()->ActorHasTag("Platform")) { if (OutTraceResultDownRigthF[i].GetActor()->ActorHasTag("MobilePlatform")) { AMobilePlatform *movil = dynamic_cast<AMobilePlatform *>(OutTraceResultDownRigthF[i].GetActor()); if (movil) { if (m_currentMobile) if (m_currentMobile != movil) m_currentMobile->SetPlayerOn(false); m_currentMobile = movil; m_currentMobile->SetPlayerOn(true); } } else { if (m_currentMobile) { m_currentMobile->SetPlayerOn(false); m_isOnMobilePlatform = false; m_currentMobile = nullptr; } } action = true; FVector localizaion = OutTraceResultDownRigthF[i].Location; SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultDownRigthF[i].Location, -m_capsuleHeight)); m_hasLanded = true; m_isFalling = false; m_actualJumpSpeed = JumpSpeed; break; } } if (!action) { if (m_currentMobile) { m_currentMobile->SetPlayerOn(false); m_isOnMobilePlatform = false; m_currentMobile = nullptr; } m_hasLanded = false; } newLocationUp.X = (FMath::Abs(GetActorUpVector().X) <= 0.01) ? m_lastPosition.X : GetActorLocation().X; newLocationUp.Y = (FMath::Abs(GetActorUpVector().Y) <= 0.01) ? m_lastPosition.Y : GetActorLocation().Y; newLocationUp.Z = (FMath::Abs(GetActorUpVector().Z) <= 0.01) ? m_lastPosition.Z : GetActorLocation().Z; //const FVector EndTraceUp = newLocationUp + GetActorUpVector() * m_capsuleHeight; const FVector EndTraceUpLeftF = (newLocationUp + GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) + GetActorUpVector() * m_capsuleHeight; const FVector EndTraceUpRightF = (newLocationUp - GetActorForwardVector() * (m_capsuleRadious - m_capsuleRadiousPadding)) + GetActorUpVector() * m_capsuleHeight; //GetWorld()->LineTraceMulti(OutTraceResultUp, StartTrace, EndTraceUp, COLLISION_PLAYER, m_TraceParams, ResponseParam); //DrawDebugLine(GetWorld(), StartTrace, EndTraceUp, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); //bool collisionUp = OutTraceResultUp.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultUpLeftF, StartTraceLeftF, EndTraceUpLeftF, COLLISION_PLAYER, m_TraceParams, ResponseParam); //DrawDebugLine(GetWorld(), StartTraceLeftF, EndTraceUpLeftF, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); bool collisionUpLeftF = OutTraceResultUpLeftF.Num() > 0; GetWorld()->LineTraceMulti(OutTraceResultUpRigthF, StartTraceRigthF, EndTraceUpRightF, COLLISION_PLAYER, m_TraceParams, ResponseParam); //DrawDebugLine(GetWorld(), StartTraceRigthF, EndTraceUpRightF, FColor(1.0f, 0.f, 0.f, 1.f), false, 10.f); bool collisionUpRightF = OutTraceResultUpRigthF.Num() > 0; if (collisionUpLeftF) { int size = OutTraceResultUpLeftF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultUpLeftF[i].GetActor()->ActorHasTag("Platform")) { if (OutTraceResultUpLeftF[i].GetActor()->ActorHasTag("MobilePlatform")) SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultUpLeftF[i].Location, m_capsuleHeight + 20)); else SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultUpLeftF[i].Location, m_capsuleHeight)); m_headCollision = true; m_actualJumpSpeed = 0.0f; break; } } else if (collisionUpRightF) { int size = OutTraceResultUpRigthF.Num(); for (int i = 0; i < size; i++) if (OutTraceResultUpRigthF[i].GetActor()->ActorHasTag("Platform")) { if (OutTraceResultUpRigthF[i].GetActor()->ActorHasTag("MobilePlatform")) SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultUpRigthF[i].Location, m_capsuleHeight + 20)); else SetActorLocation(RecalculateLocation(GetActorUpVector(), GetActorLocation(), OutTraceResultUpRigthF[i].Location, m_capsuleHeight)); m_headCollision = true; m_actualJumpSpeed = 0.0f; break; } } }