// 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