void AFPSHorrorCharacter::OnFire() { // LINE TRACE STUFF FCollisionQueryParams Traceparam; FCollisionObjectQueryParams CollisionObjectParams; FVector Start = FirstPersonCameraComponent->GetComponentLocation(); //FVector Start = FirstPersonCameraComponent FVector End = Start + FirstPersonCameraComponent->GetForwardVector() * range; FHitResult HitData(ForceInit); //ignore collision with player AFPSHorrorCharacter* myCharacter = Cast<AFPSHorrorCharacter>(this); Traceparam.AddIgnoredActor(myCharacter); GetWorld()->LineTraceSingle(HitData, Start, End, Traceparam, CollisionObjectParams); //Check the target's hit by the line trace if (HitData.GetActor() != NULL) { AGuard* newGuard = Cast<AGuard>(HitData.GetActor()); if (newGuard) { newGuard->Health -= Damage; if (newGuard->Health <= 0)//increase the power meter if the guard is dead { CurrentPower += 10; IncreaseCurrentHealth(); CheckPower(); } } } }
// Called every frame void ASwoim::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (CurrentHealth < 0) return; acceleration = FVector(0, 0, 0); FVector NewLocation = GetActorLocation(); FVector HitLocation; UWorld* const World = GetWorld(); if (SwoimController.IsValid()) { center = SwoimController->center; center.Z = 300; //UE_LOG(LogTemp, Warning, TEXT("SwoimController is Valid")); } FVector cen = seek(center); //Track mouse FVector sep = separate(); //Move away from other swoimers FVector ali = align(); // aligin with other swoimers FVector coh = cohesion(); // move towards the CM of the swoim FVector atk = FVector(0,0,0); // move toward target //UE_LOG(LogTemp, Warning, TEXT("swoimer attacking %s"), targetSwoimer); if (targetSwoimer != NULL) { atk = attack(targetSwoimer); } FHitResult HitData(ForceInit); if (TraceAhead(NewLocation, NewLocation + LookAheadDistance * DeltaTime * velocity, World, HitData)) { if (!HitData.GetActor()->GetClass()->IsChildOf(ASwoim::StaticClass())) { FVector ImpactNormalVec = HitData.ImpactNormal; FVector DirectionToAvoidImpact = ImpactNormalVec - velocity.GetSafeNormal() * FVector::DotProduct(ImpactNormalVec, velocity.GetSafeNormal()); avoidAhead = DirectionToAvoidImpact / (HitData.Distance - 20); //UE_LOG(LogTemp, Warning, TEXT("mesh ahead, avoid at dir X %f"), avoid.X); //UE_LOG(LogTemp, Warning, TEXT("mesh ahead, avoid at dir Y %f"), avoid.Y); //UE_LOG(LogTemp, Warning, TEXT("mesh ahead, avoid at dir Z %f"), avoid.Z); //UE_LOG(LogTemp, Warning, TEXT("mesh ahead, distance %f"), HitData.Distance); } } FVector avoidClosest = FVector(0, 0, 0); //if (ActorGetDistanceToCollision(NewLocation, ECollisionChannel::ECC_WorldStatic, avoidClosest) > 0) { // if (avoidClosest.Size() < 200){ // avoidClosest = NewLocation - avoidClosest; // } //} //center = center + 30 * DeltaTime*FVector(-FMath::Sin(DeltaTime), FMath::Cos(DeltaTime), 0); sep = sep * SepFactor; ali = ali * AliFactor; coh = coh * CohFactor; cen = cen * CenFactor; atk = atk * AtkFactor; FVector avoid = (avoidAhead)* AvoFactor1 + avoidClosest * AvoFactor2; acceleration = sep + ali + coh + cen + avoid + atk; avoidAhead = avoidAhead / LookAheadDecay; if (acceleration.Size() > Forcelimit) { acceleration = acceleration.GetUnsafeNormal() * Forcelimit; } velocity = velocity + acceleration; if (velocity.Size() > Speedlimit) { velocity = velocity.GetSafeNormal() * Speedlimit; } //if (velocity.Size() < 0.3 * Speedlimit) { // velocity = velocity.GetSafeNormal() * 0.3 * Speedlimit; //} NewLocation = NewLocation + velocity * DeltaTime; FHitResult* SweepHitData = &HitData; while (!SetActorLocation(NewLocation, true, SweepHitData)) { FVector MoveToAvoidHit = SweepHitData->ImpactNormal; NewLocation = NewLocation + MoveToAvoidHit * 0.1; } SetActorRotation(velocity.Rotation() + FRotator(-90, 0, 0)); }