Unreal教学(4)——组件和碰撞(Components and Collision)

来源:互联网 发布:java二分递归 编辑:程序博客网 时间:2024/06/01 18:38

这个算是第一个比较深入到Unreal引擎编码的例子,这个例子中,可以通过A,S,D,W控制球体的移动,通过鼠标控制转向,有燃烧的火球,非常的逼真。

下面是运行的示例图,跑动的时候效果会更赞,可以看到燃烧的火球:

先贴上四个文件的代码,然后再来分析。

CollidingPawn.h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "GameFramework/Pawn.h"#include "Particles/ParticleSystemComponent.h"#include "CollidingPawn.generated.h"UCLASS()class QUICKSTART_API ACollidingPawn : public APawn{GENERATED_BODY()public:// Sets default values for this pawn's propertiesACollidingPawn();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;UParticleSystemComponent* OurParticleSystem;class UCollidingPawnMovementComponent* OurMovementComponent;virtual UPawnMovementComponent* GetMovementComponent() const override;void MoveForward(float AxisValue);void MoveRight(float AxisValue);void Turn(float AxisValue);void ParticleToggle();};

CollidingPawn.cpp

// Fill out your copyright notice in the Description page of Project Settings.#include "QuickStart.h"#include "CollidingPawn.h"#include "CollidingPawnMovementComponent.h"#include "Components/SphereComponent.h"#include "UObject/ConstructorHelpers.h"#include "GameFramework/SpringArmComponent.h"#include "Camera/CameraComponent.h"// Sets default valuesACollidingPawn::ACollidingPawn(){ // 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.0f);    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->SetupAttachment(RootComponent);    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));    if (SphereVisualAsset.Succeeded())    {        SphereVisual->SetStaticMesh(SphereVisualAsset.Object);        SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));        SphereVisual->SetWorldScale3D(FVector(0.8f));    }    // Create a particle system that we can activate or deactivate    OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));    OurParticleSystem->SetupAttachment(SphereVisual);    OurParticleSystem->bAutoActivate = false;    OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));    static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));    if (ParticleAsset.Succeeded())    {        OurParticleSystem->SetTemplate(ParticleAsset.Object);    }    // Use a spring arm to give the camera smooth, natural-feeling motion.    USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));    SpringArm->SetupAttachment(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* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));    Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);    // Take control of the default player    AutoPossessPlayer = EAutoReceiveInput::Player0;// Create an instance of our movement component, and tell it to update our root component.    OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustomMovementComponent"));    OurMovementComponent->UpdatedComponent = RootComponent;}// Called when the game starts or when spawnedvoid ACollidingPawn::BeginPlay(){Super::BeginPlay();}// Called every framevoid ACollidingPawn::Tick(float DeltaTime){Super::Tick(DeltaTime);}// Called to bind functionality to inputvoid ACollidingPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);    InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);    InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);    InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);}UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const{    return OurMovementComponent;}void ACollidingPawn::MoveForward(float AxisValue){    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))    {        OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);    }}void ACollidingPawn::MoveRight(float AxisValue){    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))    {        OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);    }}void ACollidingPawn::Turn(float AxisValue){    FRotator NewRotation = GetActorRotation();    NewRotation.Yaw += AxisValue;    SetActorRotation(NewRotation);}void ACollidingPawn::ParticleToggle(){    if (OurParticleSystem && OurParticleSystem->Template)    {        OurParticleSystem->ToggleActive();    }}

CollidingPawnMovementComponent.h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "GameFramework/PawnMovementComponent.h"#include "CollidingPawnMovementComponent.generated.h"/** *  */UCLASS()class QUICKSTART_API UCollidingPawnMovementComponent : public UPawnMovementComponent{GENERATED_BODY()public:virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;};

CollidingPawnMovementComponent.cpp

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "GameFramework/PawnMovementComponent.h"#include "CollidingPawnMovementComponent.generated.h"/** *  */UCLASS()class QUICKSTART_API UCollidingPawnMovementComponent : public UPawnMovementComponent{GENERATED_BODY()public:virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;};


阅读全文
0 0
原创粉丝点击