// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "SpaceShip.generated.h"
class USphereComponent;
class UCameraComponent;
class USpringArmComponent;
UCLASS()
class SPACESHIPBATTLE_API ASpaceShip : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ASpaceShip();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, Category = "Component");
UStaticMeshComponent* ShipSM;
UPROPERTY(VisibleAnywhere, Category = "Component");
USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, Category = "Component");
UCameraComponent* CameraComp;
UPROPERTY(VisibleAnywhere, Category = "Component");
USpringArmComponent* SpringArmComp;
APlayerController* PC;
void LookAtCursor();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
SpaceShip.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpaceShip.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/KismetMathLibrary.h"
#include "GameFramework/SpringArmComponent.h"
// Sets default values
ASpaceShip::ASpaceShip()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
ShipSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipSM"));
RootComponent = ShipSM;
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionCom"));
CollisionComp->SetupAttachment(RootComponent);
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
SpringArmComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraCom"));
CameraComp->SetupAttachment(SpringArmComp);
}
// Called when the game starts or when spawned
void ASpaceShip::BeginPlay()
{
Super::BeginPlay();
PC = Cast<APlayerController>(GetController());
PC->bShowMouseCursor = true;
}
void ASpaceShip::LookAtCursor()
{
FVector MouseLocation, MouseDirection;
PC->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
FVector TargetLocation = FVector(MouseLocation.X, MouseLocation.Y, GetActorLocation().Z);
FRotator Rotator = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), TargetLocation);
SetActorRotation(Rotator);
}
// Called every frame
void ASpaceShip::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
LookAtCursor();
}
// Called to bind functionality to input
void ASpaceShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}