Hi everyone! I started recently with UE4 and im making a TopDownShooter, I made that the character can spawn the bullet,the problem is that this bullet isnt go to the cursor, I try everything i know.I wanna know how you can solve this problem,thank you!!
The code:
MyPlayer.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Character.h"
#include "MyPlayer.generated.h"
UCLASS()
class TDS_API AMyPlayer : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyPlayer();
/*INPUTS*/
void MoveFoward(float val);
void MoveRight(float val);
//Gun muzzle's offset from the camera location.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
FVector MuzzleOffset;
//Projectile class to spawn
UPROPERTY(EditDefaultsOnly, Category = Projectile)
TSubclassOf<class ATDSProjectile> ProjectileClass;
/*OVERRIDES*/
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UCameraComponent* PlayerCamera;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USpringArmComponent* cameraPoint;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* staticMesh;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
public:
UFUNCTION()
void Fire();
};
MyPlayer.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TDS.h"
#include "MyPlayer.h"
#include "TDSProjectile.h"
// Sets default values
AMyPlayer::AMyPlayer() {
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
cameraPoint = CreateDefaultSubobject<USpringArmComponent>(TEXT("cameraPoint"));
cameraPoint->AttachTo(RootComponent);
PlayerCamera->AttachTo(cameraPoint);
staticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
staticMesh->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void AMyPlayer::BeginPlay() {
Super::BeginPlay();
APlayerController* MyController = GetWorld()->GetFirstPlayerController();
MyController->bShowMouseCursor = true;
}
// Called every frame
void AMyPlayer::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
if (Controller && Controller->IsLocalController()) {
APlayerController* PC = Cast<APlayerController>(Controller);
FVector MouseLocation, MouseDirection;
PC->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
SetActorRotation(FRotator(0, MouseDirection.Rotation().Yaw, 0));
}
}
// Called to bind functionality to input
void AMyPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAxis("MoveFoward", this, &AMyPlayer::MoveFoward);
InputComponent->BindAxis("MoveRight", this, &AMyPlayer::MoveRight);
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPlayer::Fire);
}
void AMyPlayer::MoveFoward(float val) {
//FRotator Rotation(0, GetActorRotation().Yaw,0);
FVector Foward(1, 0, 0); //= FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
AddMovementInput(Foward, val);
}
void AMyPlayer::MoveRight(float val) {
//FRotator Rotation(0, GetActorRotation().Yaw, 0);
FVector Right(0, 1, 0); //= FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
AddMovementInput(Right, val);
}
void AMyPlayer::Fire() {
//Attempt to fire a projectile
if (ProjectileClass) {
//Get camera transform
FVector CameraLocation;
FRotator CameraRotation;
GetActorEyesViewPoint(CameraLocation, CameraRotation);
//Transform MuzzleOffset from camera space to world space.
FVector MuzzleLocation = CameraLocation + FTransform(CameraRotation).TransformVector(MuzzleOffset);
FRotator MuzzleRotation = CameraRotation;
//Skew the aim to be slightly upwards
MuzzleRotation.Pitch += 10.0f;
UWorld* World = GetWorld();
if (World) {
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
//Spawn the projectiles at the muzzle
ATDSProjectile* Projectile = World->SpawnActor<ATDSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile) {
//Set projectile's initial trajectory
FVector LaunchDirection = MuzzleRotation.Vector();
Projectile->FireInDirection(LaunchDirection);
}
}
}
}