Spawned weapon trace issue

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;
        }

Can you share the code with the trace?

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);

Since its VR, I would try this with the weapon actor forward vector, not the owner.

1 Like