实现平台:win8.1 UE4.10
按教程练习。
----------------------------------------------
1、实现Pawn移动(input)
MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class MYPROJECT2_BP_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
// 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* InputComponent) override;
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
void MoveForward(float Value);
void MoveRight(float Value);
void Bigger();
void Smaller();
bool bGrowing;
FVector CurrentVelocity;
};
Mypawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2_BP.h"
#include "MyPawn.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
AutoPossessPlayer = EAutoReceiveInput::Player0;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
UCameraComponent* Mycamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
Mycamera->AttachTo(RootComponent);
Mycamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
Mycamera->SetRelativeRotation(FRotator(-45.0f, 0, 0));
OurVisibleComponent->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
// 在一秒的时间内增长到两倍的大小
CurrentScale += DeltaTime;
}
else
{
// 随着增长收缩到一半
CurrentScale -= (DeltaTime * 0.5f);
}
// 确认永不低于起始大小,或增大之前的两倍大小。
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
// 基于"MoveX"和 "MoveY"坐标轴来处理移动
if (!CurrentVelocity.IsZero())
{
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAction("Bigger", IE_Pressed, this, &AMyPawn::Bigger);
InputComponent->BindAction("Smaller", IE_Pressed, this, &AMyPawn::Smaller);
InputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
}
void AMyPawn::MoveForward(float Value)
{
CurrentVelocity.X = Value * 100.0f;
}
void AMyPawn::MoveRight(float Value)
{
CurrentVelocity.Y = Value * 100.0f;
}
void AMyPawn::Bigger()
{
bGrowing = true;
}
void AMyPawn::Smaller()
{
bGrowing = false;
}
==============================================================================
2、实现相机自动切换(注意需要两个camera)
CameraDirector.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"
UCLASS()
class MYPROJECT2_BP_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACameraDirector();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere)
AActor* CameraOne;
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
float TimeToNextCameraChange;
};
CameraDirector.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2_BP.h"
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ACameraDirector::ACameraDirector()
{
// 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;
}
// Called when the game starts or when spawned
void ACameraDirector::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACameraDirector::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
const float TimeBetweenCameraChanges = 2.0f;
const float SmoothBlendTime = 0.75;
TimeToNextCameraChange -= DeltaTime;
if (TimeToNextCameraChange<=0.0f)
{
TimeToNextCameraChange += TimeBetweenCameraChanges;
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController)
{
if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
{
// 立即切换到相机1。
OurPlayerController->SetViewTarget(CameraOne);
}
else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
{
// 平滑地混合到相机2。
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
}
}
}
}
==============================================================================
3、实现倒计时(timer)(注意宏的用法、C++与bluepringt交互)
CountDown.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "CountDown.generated.h"
UCLASS()
class MYPROJECT2_BP_API ACountDown : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACountDown();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere, Category = "CountNumber")
int32 CountDownTime;
UTextRenderComponent* CountDownText;
void UpdataTimerDisplay();
void AdvanceTimer();
UFUNCTION(BlueprintNativeEvent, Category = "CountNumber")
void CountdownHasFinish();
virtual void CountdownHasFinish_Implementation();
FTimerHandle CountdownTimerhandle;
};
CountDown.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2_BP.h"
#include "CountDown.h"
// Sets default values
ACountDown::ACountDown()
{
// 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;
CountDownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
CountDownText->SetHorizontalAlignment(EHTA_Center);
CountDownText->SetWorldSize(150.0f);
RootComponent = CountDownText;
CountDownTime = 3.0f;
}
void ACountDown::UpdataTimerDisplay()
{
CountDownText->SetText((FString::FromInt(FMath::Max(CountDownTime, 0))));
}
void ACountDown::AdvanceTimer()
{
--CountDownTime;
UpdataTimerDisplay();
if (CountDownTime<1)
{
GetWorldTimerManager().ClearTimer(CountdownTimerhandle);
CountdownHasFinish();
}
}
//void ACountDown::CountdownHasFinish()
//{
// //CountDownText->SetText(TEXT("GO!"));
//}
void ACountDown::CountdownHasFinish_Implementation()
{
//Change to a special readout
CountDownText->SetText(TEXT("GO!"));
}
// Called when the game starts or when spawned
void ACountDown::BeginPlay()
{
Super::BeginPlay();
UpdataTimerDisplay();
GetWorldTimerManager().SetTimer(CountdownTimerhandle, this, &ACountDown::AdvanceTimer, 1.0f, true);
}
// Called every frame
void ACountDown::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
==============================================================================
4、Pwan与碰撞(粒子特效) 注意碰撞时,按下space才会产生火焰
ColisionPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Pawn.h"
#include "ColisionPawn.generated.h"
UCLASS()
class MYPROJECT2_BP_API AColisionPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AColisionPawn();
// 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* InputComponent) override;
UParticleSystemComponent* MyParticleSystem;
class UColisionPawnMovementComponent* MyMovementComponent;
virtual UPawnMovementComponent* GetMovementComponent() const override;
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
void Turn(float AxisValue);
void ParticleToggle();
};
ColisionPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2_BP.h"
#include "ColisionPawn.h"
#include "ColisionPawnMovementComponent.h"
// Sets default values
AColisionPawn::AColisionPawn()
{
// 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;
// Our root component will be a sphere that reacts to physics
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.f);
SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
// Create and position a mesh component so we can see where our sphere is
UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
SphereVisual->AttachTo(SphereComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded())
{
SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
SphereVisual->SetRelativeLocation(FVector(0, 0, 0));
SphereVisual->SetWorldScale3D(FVector(0.8f));
}
// Create a particle system that we can activate or deactivate
MyParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticle"));
MyParticleSystem->AttachTo(SphereVisual);
MyParticleSystem->bAutoActivate = false;
MyParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
if (ParticleAsset.Succeeded())
{
MyParticleSystem->SetTemplate(ParticleAsset.Object);
}
// Use a spring arm to give the camera smooth, natural-feeling motion.
USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));
SpringArm->AttachTo(RootComponent);
SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f);
SpringArm->TargetArmLength = 400.0f;
SpringArm->bEnableCameraLag = true;
SpringArm->CameraLagSpeed = 3.0f;
// Create a camera and attach to our spring arm
UCameraComponent* MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("AcrualCamera"));
MyCamera->AttachTo(SpringArm, USpringArmComponent::SocketName);
// Take control of the default player
AutoPossessPlayer = EAutoReceiveInput::Player0;
// Create an instance of our movement component, and tell it to update the root.
MyMovementComponent = CreateDefaultSubobject<UColisionPawnMovementComponent>(TEXT("CustomMovementComponent"));
MyMovementComponent->UpdatedComponent = RootComponent;
}
UPawnMovementComponent* AColisionPawn::GetMovementComponent() const
{
return MyMovementComponent;
}
void AColisionPawn::MoveForward(float AxisValue)
{
if (MyMovementComponent && (MyMovementComponent->UpdatedComponent == RootComponent))
{
MyMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
}
}
void AColisionPawn::MoveRight(float AxisValue)
{
if (MyMovementComponent && (MyMovementComponent->UpdatedComponent == RootComponent))
{
MyMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
}
}
void AColisionPawn::Turn(float AxisValue)
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += AxisValue;
SetActorRotation(NewRotation);
}
void AColisionPawn::ParticleToggle()
{
if (MyParticleSystem && MyParticleSystem->Template)
{
MyParticleSystem->ToggleActive();
}
}
// Called when the game starts or when spawned
void AColisionPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AColisionPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void AColisionPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &AColisionPawn::ParticleToggle);
InputComponent->BindAxis("MoveForward", this, &AColisionPawn::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AColisionPawn::MoveRight);
InputComponent->BindAxis("Turn", this, &AColisionPawn::Turn);
}
ColisionPawnMovementComponent.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/PawnMovementComponent.h"
#include "ColisionPawnMovementComponent.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT2_BP_API UColisionPawnMovementComponent : public UPawnMovementComponent
{
GENERATED_BODY()
public:
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction);
};
ColisionPawnMovementComponent.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2_BP.h"
#include "ColisionPawnMovementComponent.h"
void UColisionPawnMovementComponent::TickComponent(float DeltaTime,