Made Movement Componenet and got collision act to move

This commit is contained in:
LukTeg 2023-10-16 19:59:56 -04:00
parent 36e11b4aae
commit 3b250c5dd0
9 changed files with 109 additions and 7 deletions

BIN
Content/Blueprints/Enemy1.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Materials/wood.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Mesh/SM_Cruise_Ship.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -0,0 +1,47 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MoveComponent.h"
// Sets default values for this component's properties
UMoveComponent::UMoveComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UMoveComponent::BeginPlay()
{
Super::BeginPlay();
// Set start location
StartRelativeLocation = this->GetRelativeLocation();
//Compute normalized movement
MoveOffsetNorm = MoveOffset;
MoveOffsetNorm.Normalize();
MaxDistance = MoveOffset.Length();
//Check if ticking required
SetComponentTickEnabled(MoveEnable);
}
// Called every frame
void UMoveComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//Set the current distance
if (MoveEnable)
{
CurDistance += DeltaTime * Speed * MoveDirection;
if (CurDistance >= MaxDistance || CurDistance <= 0.0f) {
MoveDirection *= -1;
}
}
// Compute and set current location
SetRelativeLocation(StartRelativeLocation + MoveOffsetNorm * CurDistance);
}

View File

@ -0,0 +1,46 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "MoveComponent.generated.h"
UCLASS(ClassGroup = (Actors), meta = (BlueprintSpawnableComponent))
class JUMPINGSHIP_API UMoveComponent : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UMoveComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
//Offset to move
UPROPERTY(EditAnywhere)
FVector MoveOffset;
private:
friend class FMoveComponentVisualizer;
// Enables the movement of the component
UPROPERTY(EditAnywhere)
bool MoveEnable = true;
//Speed
UPROPERTY(EditAnywhere)
float Speed = 1.0f;
//Computed locations
FVector StartRelativeLocation;
FVector MoveOffsetNorm;
float MaxDistance = 0.0f;
float CurDistance = 0.0f;
float MoveDirection = 1;
};