When i click play, my character is locked to a 180 degree rotation, that means i cant rotate my view point with the mouse to the AI characters and shoot them unless they spawn in front of me. This just started happening after i refactored my pull trigger function in Gun.cpp.
Could you show your code? Please use a code block when doing so by using the </> button.
Gun.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class SIMPLESHOOTER_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnywhere)
USceneComponent* Root;
UPROPERTY(VisibleAnywhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditDefaultsOnly)
UParticleSystem* ShootParticle;
UPROPERTY(EditDefaultsOnly)
USoundBase* ShootSound;
UPROPERTY(EditDefaultsOnly)
UParticleSystem* ImpactParticle;
UPROPERTY(EditDefaultsOnly)
USoundBase* ImpactSound;
AController* ShooterController;
APawn* ShooterCharacter;
UPROPERTY(EditAnywhere)
float MaxRange = 1000.f;
float Damage = 10.f;
bool GunTrace(FHitResult& Hit, FVector& ParticleHitDirection);
AController* GetOwnerController();
};
Gun.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AGun::AGun()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent> ("Root");
RootComponent = Root;
Mesh = CreateDefaultSubobject<USkeletalMeshComponent> ("Mesh");
Mesh -> SetupAttachment(Root);
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AGun::PullTrigger()
{
UGameplayStatics::SpawnEmitterAttached(ShootParticle, Mesh, TEXT("MuzzleFlashSocket"));
UGameplayStatics::SpawnSoundAttached(ShootSound, Mesh, TEXT("MuzzleFlashSocket"));
FHitResult Hit;
FVector ParticleHitDirection;
bool bSuccess = GunTrace(Hit, ParticleHitDirection);
if (bSuccess)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticle, Hit.Location, ParticleHitDirection.Rotation());
UGameplayStatics::PlaySoundAtLocation(GetWorld(), ImpactSound, Hit.Location);
AActor* HitActor = Hit.GetActor();
if (HitActor != nullptr)
{
FPointDamageEvent DamageType(Damage, Hit, ParticleHitDirection, nullptr);
GetOwnerController();
HitActor -> TakeDamage(Damage, DamageType, ShooterController, this);
}
}
}
bool AGun::GunTrace(FHitResult& Hit, FVector& ParticleHitDirection)
{
ShooterCharacter = Cast<APawn> (GetOwner());
if(!ShooterCharacter) {return false;}
ShooterController = ShooterCharacter -> GetController();
if(!ShooterController) {return false;}
FVector Location;
FRotator Rotation;
ShooterController -> GetPlayerViewPoint(Location, Rotation);
ParticleHitDirection = -Rotation.Vector();
FVector LineTraceEnd = Location + Rotation.Vector() * MaxRange;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
Params.AddIgnoredActor(ShooterCharacter);
return GetWorld() -> LineTraceSingleByChannel(
Hit,
Location,
LineTraceEnd,
ECollisionChannel::ECC_GameTraceChannel1,
Params
);
}
AController* AGun::GetOwnerController()
{
ShooterCharacter = Cast<APawn> (GetOwner());
if(!ShooterCharacter) {return nullptr;}
ShooterController = ShooterCharacter -> GetController();
if(!ShooterController) {return false;}
return ShooterController;
}
And this issue goes away if you remove the gun?
No, i’ve not tried that
Okay…i just tried it. The problem persists, i guess it had nothing to do with the Gun Class
What about your Input Mapping settings?
Could you send me your project by using the following link?
Please use File > Package Project > Zip Up Project for creating the zip as it will only include necessary files.