虚幻4 ue4 学习笔记pwan篇 1.1b 创建staticmeshcomponent 静态网格组件

时间:2022-12-01 20:52:08
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyPawn.h"
#include "Runtime/Engine/Classes/Components/BoxComponent.h"//    UBoxComponent 引用
#include "Runtime/Engine/Classes/Components/SphereComponent.h"//    USphereComponent 引用
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h" // ConstructorHelpers 引用
// 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;
    UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));//无视觉效果
    SphereVisual->SetupAttachment(RootComponent);//添加附件到RootComponent    无视觉效果

    //这里可以用过在ue4 赋值引用得到地址 不是物理路径
    //FObjectFinder是ConstructorHelpers的内部类 
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAssetWzh(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));//打开素材库

    if (SphereVisualAssetWzh.Succeeded())//判断素材库是否打开
    {
        SphereVisual->SetStaticMesh(SphereVisualAssetWzh.Object);//设置素材 这里有视觉效果
        SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));//设置位置0,0,0为初始位置    初始位置为防止位置 z轴与地平线一致 在根组件没有赋予形状时                                                                                                                      
        SphereVisual->SetWorldScale3D(FVector(0.8f));//SetWorldScale3D 设置组件在世界空间中的变换倍数。 倍数以原来模型大小为基准
    }

}

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

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

}

 staticmeshcomponent只有视觉效果 没有物理碰撞 需要结合 物体组件例如spherecomponent

虚幻4 ue4 学习笔记pwan篇 1.1b 创建staticmeshcomponent 静态网格组件