【UnrealEngine】官方编程快速入门遇到的坑

官方编程快速入门文档链接

添加如下代码,VS出现红线错误,或输入过程VS不跳语句补全。

原因:没有include相应的头文件。

UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;


VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);


static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

解决方法:

在.h文件中加入如下内容。必须注意头文件引用的顺序,详情请自行摸索。

#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "UObject/ConstructorHelpers.h"

遇到相应情况,请自行到官方API中查找相应组件需要Include的头文件路径。

 

 

在完成编译后,C++类预览还是处于Actor的球体状态,请查看是否有导入StarterContent资源包。

解决方法,①创建项目时候就选择了StarterContent资源包。(创建项目前可以这样操作,否则按②的操作)

                  ②Add New->AddFeature or Content Pack...->Content Pack ->StarterContent   (如图)

https://blog.csdn.net/Terrell21

下面为能正常运行的源码:

FloatingActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "UObject/ConstructorHelpers.h"
#include "FloatingActor.generated.h"


UCLASS()
class INICONFIG_API AFloatingActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFloatingActor();

	UPROPERTY(VisibleAnyWhere)
	UStaticMeshComponent* VisualMesh;
	
	UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "FloatingActor")		//编辑可见,蓝图可读写,标签名 
		float FloatSpeed = 20.0f;

	UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "FloatingActor")
		float RotationSpeed = 20.0f;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

FloatingActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
 	// 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;
	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));    //创建静态网格组件
	VisualMesh->SetupAttachment(RootComponent);									//将静态网格组件设置到根组件下。

	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
	//调用结构帮助器查找目标静态网格体。

	//检测是否成功查找到
	if (CubeVisualAsset.Succeeded() ){
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);				//查找成功,将查找到的静态网格设置到静态网格组件中。
		VisualMesh->SetRelativeLocation(FVector(0, 0, 0));				//然后对静态网格组件设置相对位置。
	}

	

}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	FVector NewLocation = GetActorLocation();			//获取该帧位置
	FRotator NewRotation = GetActorRotation();			//获取该帧旋转量
	float RunningTime = GetGameTimeSinceCreation();		//获取时间(场景开始了多久的时间)
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	NewLocation.Z += DeltaHeight * FloatSpeed;						//DeltaHeight 在-1 到 1的范围
	float DeltaRotation = DeltaTime * RotationSpeed;
	NewRotation.Yaw -= DeltaRotation;			//绕Z轴旋转, 0不转   + 向北转(顺时针)  - 向南转(逆时针)
	SetActorLocationAndRotation(NewLocation, NewRotation);

}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This is the eBook of the printed book and may not include any media, website access codes, or print supplements that may come packaged with the bound book. “With his YouTube channel, Mitch’s VR Lab, Mitch has helped thousands of people understand the foundations of locomotion and interaction mechanics with clear and concise UE4 videos. I’m thrilled that he has taken the time to bring all his knowledge and experience in working with Unreal Engine and Virtual Reality to the Unreal? Engine VR Cookbook…. Mitch is uniquely qualified to share this book with the world.” —Luis Cataldi, Unreal Engine Education, Epic Games, Inc. For game developers and visualization specialists, VR is the next amazing frontier to conquer—and Unreal Engine 4 is the ideal platform to conquer it with. Unreal ? Engine VR Cookbook is your complete, authoritative guide to building stunning experiences on any Unreal Engine 4-compatible VR hardware. Renowned VR developer and instructor Mitch McCaffrey brings together best practices, common interaction paradigms, specific guidance on implementing these paradigms in Unreal Engine, and practical guidance on choosing the right approaches for your project. McCaffrey’s tested “recipes” contain step-by-step instructions, while empowering you with concise explanations of the underlying theory and math. Whether you’re creating first-person shooters or relaxation simulators, the techniques McCaffrey explains help you get immediate results, as you gain “big picture” knowledge and master nuances that will help you succeed with any genre or project. Understand basic VR concepts and terminology Implement VR logic with Blueprint visual scripting Create basic VR projects with Oculus Rift, HTC Vive, Gear VR, Google VR, PSVR, and other environments Recognize and manage differences between seated and standing VR experiences Set up trace interactions and teleportation Work with UMG and 2D UIs Implement character inverse kinematics (IK) for head and hands Define ef

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值