I am trying to make a turret for a vehicle, but once I shoot the projectile I don’t see it and then proceed to realize that it’s shooting mid-air opposite to my camera. Can someone help me? Here’s my headers:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include <OtherProjectWouldNot/GunnerProjectile.h>
#include "CoreMinimal.h"
#include "WheeledVehicle.h"
#include "MyWheeledVehicle.generated.h"
/**
*
*/
class UGunnerProjectile;
UCLASS()
class OTHERPROJECTWOULDNOT_API AMyWheeledVehicle : public AWheeledVehicle
{
GENERATED_BODY()
AMyWheeledVehicle();
virtual void Tick(float Deltatime) override;
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
void TurnLeft(float direction);
void TurnForward(float Direction);
void ThrottleInput(float Input);
void SteerInput(float Input);
void UpdateInAirControl(float DeltaTime);
void OnPressedHandBrake();
void OnReleasedHandBrake(float DeltaTime);
void Fire();
UPROPERTY(VisibleAnywhere, Category = "Camera")
class UCameraComponent* Camera;
UPROPERTY(VisibleAnywhere, Category = "SpringArm")
class USpringArmComponent* SpringArm;
UPROPERTY(EditDefaultsOnly, Category = "Projectile Class")
TSubclassOf<AGunnerProjectile> GunnerProjectileClass;
UPROPERTY(EditAnywhere, Category = "GunMuzzle")
USkeletalMeshComponent* Gun;
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
USkeletalMeshComponent* Mesh1P;
UPROPERTY(EditAnywhere, Category = "GunMuzzle")
USceneComponent* MuzzleLocation;
UPROPERTY(EditAnywhere, Category = "GunOffset ")
FVector GunOffset;
};
/ Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GunnerProjectile.generated.h"
class USphereComponent;
class UProjectileMovementComponent;
UCLASS()
class OTHERPROJECTWOULDNOT_API AGunnerProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGunnerProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, Category = "ProjectileMovement")
UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(EditAnywhere, Category = "Collision")
USphereComponent* CollisionComp;
UPROPERTY(EditAnywhere, Category = "MeshProperty")
UStaticMeshComponent* BulletMesh;
UProjectileMovementComponent* ProjectileMovementSub() const {return ProjectileMovement;}
USphereComponent* CollisionCompSub() const { return CollisionComp; }
//returns their sub object
UFUNCTION()
void OnHit(UPrimitiveComponent* Component, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);
void Fire(const FVector& ShotDirection);
};
Then cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyWheeledVehicle.h"
#include "WheeledVehicleMovementComponent4W.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/InputComponent.h"
#include "GameframeWork/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
static const FName NAME_SteerInput("MoveRight");
static const FName NAME_ThrottleInput("MoveForward");
AMyWheeledVehicle::AMyWheeledVehicle()
{
UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());
/** Clamp normalized tire load to these values */
Vehicle4W->MinNormalizedTireLoad = 0;
Vehicle4W->MaxNormalizedTireLoadFiltered = 2;
Vehicle4W->MaxNormalizedTireLoad = 2;
Vehicle4W->MinNormalizedTireLoadFiltered = 0;
Vehicle4W->MaxEngineRPM = 6000;
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->Reset();
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(0,400);
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1900,500);
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(6000,400);
//changing turning rates for the back wheels
Vehicle4W->DifferentialSetup.DifferentialType = EVehicleDifferential4W::LimitedSlip_4W;
Vehicle4W->DifferentialSetup.FrontRearSplit = 0.50;
Gun = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Gun"));
Gun->SetupAttachment(Mesh1P, TEXT("gun_jnt"));
Gun->SetupAttachment(RootComponent);
MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("Muzzle Location"));
MuzzleLocation->SetupAttachment(Gun);
MuzzleLocation->SetRelativeLocation(FVector(63.0f, 86.0f, 10.0f));
Vehicle4W->TransmissionSetup.bUseGearAutoBox = true;
Vehicle4W->TransmissionSetup.GearSwitchTime = 1;
Vehicle4W->TransmissionSetup.GearAutoBoxLatency = 0.75;
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->TargetArmLength = 250;
SpringArm->bUsePawnControlRotation = true;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera Babby"));
Camera->FieldOfView = 120;
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
//GunOffset = FVector(63.0f, 86.0f, 10.0f);
}
void AMyWheeledVehicle::Tick(float Deltatime)
{
Super::Tick(Deltatime);
}
void AMyWheeledVehicle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Brakes", IE_Pressed, this, &AMyWheeledVehicle::OnPressedHandBrake);
PlayerInputComponent->BindAction("Brakes", IE_Released, this, &AMyWheeledVehicle::OnPressedHandBrake);
PlayerInputComponent->BindAxis("Turn",this, &AMyWheeledVehicle::TurnLeft);
PlayerInputComponent->BindAxis("LookUp",this,&AMyWheeledVehicle::TurnForward);
PlayerInputComponent->BindAxis(NAME_ThrottleInput,this,&AMyWheeledVehicle::ThrottleInput);
PlayerInputComponent->BindAxis(NAME_SteerInput,this,&AMyWheeledVehicle::SteerInput);
PlayerInputComponent->BindAction("Shoot", IE_Pressed, this, &AMyWheeledVehicle::Fire);
}
void AMyWheeledVehicle::UpdateInAirControl(float DeltaTime)
{
}
void AMyWheeledVehicle::TurnLeft(float direction)
{
if(direction != 0)
AddControllerPitchInput(direction);
}
void AMyWheeledVehicle::TurnForward(float Direction)
{
if(Direction != 0)
AddControllerYawInput(Direction);
}
void AMyWheeledVehicle::ThrottleInput(float Input)
{
GetVehicleMovementComponent()->SetThrottleInput(Input);
}
void AMyWheeledVehicle::SteerInput(float Input)
{
GetVehicleMovementComponent()->SetSteeringInput(Input);
}
void AMyWheeledVehicle::OnPressedHandBrake()
{
GetVehicleMovementComponent()->SetHandbrakeInput(true);
}
void AMyWheeledVehicle::OnReleasedHandBrake(float DeltaTime)
{
GetVehicleMovementComponent()->SetHandbrakeInput(true);
}
void AMyWheeledVehicle::Fire()
{
FHitResult Hit;
float WeaponRange = 200.0f;
FCollisionQueryParams QueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponRange), false, this);
UWorld* World = GetWorld();
if (GunnerProjectileClass != nullptr)
{
if (GetWorld() != nullptr)
{
const FRotator Rotator = GetControlRotation();
const FVector SpawnLocation = MuzzleLocation->GetComponentLocation();
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
AGunnerProjectile* Projectile = World->SpawnActor<AGunnerProjectile>(GunnerProjectileClass, SpawnLocation, Rotator , ActorSpawnParams);
}
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#include "GunnerProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
// Sets default values
AGunnerProjectile::AGunnerProjectile()
{
// 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;
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("Collision Component"));
CollisionComp->InitSphereRadius(15.0f);
// CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AGunnerProjectile::OnHit);
RootComponent = CollisionComp;
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
ProjectileMovement->InitialSpeed = 2000;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->MaxSpeed = 3000;
BulletMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SUM MESH"));
BulletMesh->SetupAttachment(CollisionComp);
InitialLifeSpan = 10.0f;
}
// Called when the game starts or when spawned
void AGunnerProjectile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGunnerProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AGunnerProjectile::OnHit(UPrimitiveComponent* Component, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor != this && OtherComponent->IsSimulatingPhysics())
{
OtherComponent->AddImpulseAtLocation(GetVelocity() * 100, GetActorLocation());
Destroy();
}
}
void AGunnerProjectile::Fire(const FVector& ShotDirection)
{
ProjectileMovement->Velocity = ShotDirection* ProjectileMovement->InitialSpeed;
}