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
- Unreal教学(4)——组件和碰撞(Components and Collision)
- Unreal 入门-Collision和Physics,碰撞检测
- Unreal教学(6)——玩家输入和Pawn(Player Input and Pawns)
- Unreal教学(1)——序言
- Unreal教学(9)——虚幻引擎名词解释(Unreal Engine 4 Terminology)
- Unreal教学(5)——变量,时间,事件(Variables, Timers, and Events)
- React--Components and Props(组件和属性组件)
- Unreal教学(10)——放置Actor (Placing Actors)
- 碰撞(Collision)和触发事件(Trigger)
- Unity的选择碰撞--碰撞矩阵(Collision Matrix)
- Unreal教学(2)——快速开发程序(Programming Quick Start)
- Unreal教学(3)——游戏相机控制(Game-Controlled Cameras)
- Unreal教学(7)——控制玩家相机(Player-Controlled Cameras)
- Unreal教学(8)——蓝图学习快速开始(Blueprints Quick Start Guide)
- Unreal教学(11)——物体交互(Object Interaction with Blueprints)
- Unreal教学(12)——增加玩家的能力(Enhancing Player Abilities)
- Unreal教学(13)——创建界面(Creating Screen UI Elements)
- 扬扬的J2EE学习笔记(三)Components and Roles组件和角色
- 【Scikit-Learn 中文文档】广义线性模型
- Android 开发笔记 1:Ubuntu 14.04 中安装eclipse开发环境
- HDU 1305:Immediate Decodability
- 使用Maven私服的好处
- 如何在交互式shell脚本中创建对话框
- Unreal教学(4)——组件和碰撞(Components and Collision)
- 禅道布署
- dbutil 的使用方法
- 【Linux】进程控制—exec函数族
- 记录QT的学习过程!
- Android LiveData简介(一)
- 阿里云营销引擎构建DSP/DMP系统
- 用JS锁定页面滚动条
- 基于队列的拓扑排序