|
不使用引擎自带的模板,创建一个比较纯净的工程。
打开UE4,现在创建一个空白的工程项目,命名为MyProject_1.
3.在开始游戏的时候我们需要一个游戏类型(GameMode)。所以我们需要创建一个类。点击File - Add Code to Project
4.选择创建一个GameMode类型
#pragma once #include GameFramework/GameMode.h #include MyGameMode.generated.h /** * */UCLASS()class MYPROJECT_1_API AMyGameMode : public AGameMode{ GENERATED_UCLASS_BODY() };复制代码
MyGameMode.cpp#include MyProject_1.h #include MyGameMode.h //添加Playercharacter头文件#include PlayerCharacter.h AMyGameMode::AMyGameMode(const class FPostConstructInitializeProperties PCIP) : Super(PCIP){ //设置Character类 DefaultPawnClass = APlayerCharacter::StaticClass();}复制代码
PlayerCharacter.h#pragma once #include GameFramework/Character.h #include PlayerCharacter.generated.h /** * */UCLASS()class MYPROJECT_1_API APlayerCharacter : public ACharacter{ GENERATED_UCLASS_BODY() protected: //设置玩家的输入 virtual voidSetupPlayerInputComponent(class UInputComponent* InputComponent); //定义移动函数 void MoveFrontBack(float Value); void MoveLeftRight(float Value); //定义玩家使用函数 voidUse(); //用于检测自己是不是触碰到了什么东西 boolTraceFromSelf(FHitResult OutResult,const float TraceDistance,ECollisionChannel constCollisionChanel);};复制代码
PlayerCharacter.cpp#include MyProject_1.h #include PlayerCharacter.h #include Sign.h APlayerCharacter::APlayerCharacter(const class FPostConstructInitializeProperties PCIP) : Super(PCIP){ } //=============================================玩家控制========================================================================= void APlayerCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent){ //功能函数的绑定,绑定使用按钮 InputComponent- BindAction( Use , EInputEvent::IE_Pressed,this, APlayerCharacter::Use); //和设备轴有关的设备绑定 //绑定前后移动函数 InputComponent- BindAxis( MoveFrontBack ,this, APlayerCharacter::MoveFrontBack); //玩家左右移动 InputComponent- BindAxis( MoveLeftRight ,this, APlayerCharacter::MoveLeftRight); //玩家上下镜头 InputComponent- BindAxis( LookUpDown ,this, APlayerCharacter::AddControllerPitchInput); //玩家左右镜头 InputComponent- BindAxis( LookLeftRight ,this, APlayerCharacter::AddControllerYawInput);} void APlayerCharacter::MoveFrontBack(float Value){ if (Controller Value != 0.0f) { FRotatorRotation = GetActorRotation();//获取玩家的旋转值 //判断当前玩家是否在地面上或者玩家在下落,我们保证不能玩家出现翻滚现象 if (CharacterMovement- IsMovingOnGround() || CharacterMovement- IsFalling()) { Rotation.Pitch = 0.0f; } //获取玩家轴的X轴,就是前后方向 FVector const Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X); AddMovementInput(Direction, Value);//这个玩家真实移动的调用函数 }} void APlayerCharacter::MoveLeftRight(float Value){ if (Value != 0.0f) { FRotator Rotation = GetActorRotation(); //弯曲玩家在Y走上的输入,玩家左右移动 FVectorconst Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y); //在这个方向上添加移动输入 AddMovementInput(Direction,Value); }} void APlayerCharacter::Use(){ FHitResult HitResult(EForceInit::ForceInit);//强行进行初始化 bool bTraceSuccess = TraceFromSelf(HitResult, 150.0f, ECollisionChannel::ECC_EngineTraceChannel1); if (bTraceSuccess) { AActor* const HitActor = HitResult.GetActor(); if (HitActor) { //进行类型转换 ASign*SignActor = Cast class ASign (HitActor); if (SignActor) { //让Sign打印一句消息 FString const SignMessage = SignActor- GetMessage(); GEngine- AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, SignMessage); } } }} bool APlayerCharacter::TraceFromSelf(FHitResult OutResult,const float TraceDistance, ECollisionChannel const CollisionChanel){ if (Controller) { FVector CameraLocation; FRotator CameraRotation; //获取玩家视角点 Controller- GetPlayerViewPoint(CameraLocation, CameraRotation); FVector constStartTrace = CameraLocation; //获取检测开始点 FVector constShootDirection = CameraRotation.Vector(); //把Rotation转换成Vector类型 FVector constEndTrace = StartTrace + ShootDirection*TraceDistance; //检测结束点 //设置碰撞的参数,其中最后一个参数为是否有忽略的Actor,我们选择忽略自己的碰撞,传入This FCollisionQueryParamsTraceParms(FName(TEXT( TraceFromSelf )),true,this); boolbHitReturned = false; UWorld* const World = GetWorld();//获取UWorld if (World) { //调用UWorld里面的线性检测函数用于线性检测 bHitReturned = World- LineTraceSingle(OutResult, StartTrace, EndTrace, CollisionChanel,TraceParms); } return bHitReturned; } return false;}复制代码
Sign.h/** * */UCLASS()class MYPROJECT_1_API ASign : public AActor{ GENERATED_UCLASS_BODY() //消息处理函数 UFUNCTION(BlueprintCallable, Category= Accessors ) const FString GetMessage() const; UFUNCTION(BlueprintCallable, Category= Modifiers ) voidSetMessage(const FString NewMessage); protected: UPROPERTY(EditAnywhere, Category = Messages ) FStringMessage;};复制代码
Sign.cpp#include MyProject_1.h #include Sign.h ASign::ASign(const class FPostConstructInitializeProperties PCIP) : Super(PCIP){ } const FString ASign::GetMessage() const{ return Message;} void ASign::SetMessage(const FString NewMessage){ Message = NewMessage;}复制代码
非常感谢你发贴,你好!贴子的图片显示有很大的问题! 你应该不要直接复制百度里的图片,而是将百度里的图片,另存为后发贴时点 上传图片按钮,上传后在想要插入图片的地方直接点击即可。就不会出现现在这个情况。 |
|