HHX1602
(HHX1602)
May 31, 2022, 9:28am
1
Hi,
I am having issue with my weapon’s trace. The weapon only fires at north no matter where I point it at. The issue only happens if I try to spawn it at my VR controller.
Here is the codes:
FVector HandVector = this->GetActorLocation();
FRotator HandRotator = this->GetActorRotation();
FActorSpawnParameters SpawnParam;
AutoRifle = GetWorld()->SpawnActor<ASWeapon>(RifleClass, HandVector, HandRotator, SpawnParam);
USkeletalMeshComponent* WeaponMesh = Cast<USkeletalMeshComponent>(AutoRifle->GetComponentByClass(USkeletalMeshComponent::StaticClass()));
WeaponMesh->SetSimulatePhysics(false);
AutoRifle->SetOwner(this);
AutoRifle->AttachToActor(RightController, FAttachmentTransformRules::SnapToTargetIncludingScale);
AutoRifle->AddActorLocalRotation(FQuat(FRotator(0.0, -90.0, 0.0)));
if (AutoRifle != nullptr)
{
RightController->bHasWeapon = true;
RightController->bHasRifle = true;
}
pezzott1
(pezzott1)
May 31, 2022, 10:28am
2
Can you share the code with the trace?
HHX1602
(HHX1602)
May 31, 2022, 10:30am
3
Sure thing!
FVector StartLocation = MeshComp->GetSocketLocation(MuzzleSocketName);
FVector ShotDirection = GetOwner()->GetActorForwardVector();
FVector TraceEnd = StartLocation + (ShotDirection * 10000);
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(MyOwner);
QueryParams.AddIgnoredActor(this);
QueryParams.bTraceComplex = true;
QueryParams.bReturnPhysicalMaterial = true;
FVector TracerEndPoint = TraceEnd;
FHitResult Hit;
if (GetWorld()->LineTraceSingleByChannel(Hit, StartLocation, TraceEnd, COLLISION_WEAPON, QueryParams))
{
AActor* HitActor = Hit.GetActor();
EPhysicalSurface SurfaceType = UPhysicalMaterial::DetermineSurfaceType(Hit.PhysMaterial.Get());
float ActualDamage = BaseDamage;
if (SurfaceType == SURFACE_FLESHVULNERABLE)
{
ActualDamage *= 3.0f;
}
UGameplayStatics::ApplyPointDamage(HitActor, ActualDamage, ShotDirection, Hit, GetWorld()->GetFirstPlayerController(), this, DamageTypeClass);
pezzott1
(pezzott1)
May 31, 2022, 11:08am
4
Since its VR, I would try this with the weapon actor forward vector, not the owner.
1 Like