前言:每日记录自己学习UE的心得和体会,小弟才疏学浅,如有错误的地方,欢迎大佬们指正,感谢~
蓝图案例
射线检测
使用的重要节点
Convert Mouse Location To World Space(鼠标屏幕坐标转世界坐标)
Line Trace By Channel(射线检测)
界面操作
Add to Viewport 加载到界面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B6UnQF0s-1681438840906)(null)]
UI图片修改
ui面板上的笔刷或者样式等等是可以修改图片的,可以通过蓝图SetBrush或者SetStyle
通过Tag读取模型
给模型设置tag后,可以通过tag获取
材质
按住3,点击面板可以创建颜色节点
模型
C++
相机控制脚本
AMyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class XZ_PROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere, Category = "My Pawn Component")
class UStaticMeshComponent* myStaticMesh;
UPROPERTY(VisibleAnywhere, Category = "My Pawn Component")
class UCameraComponent* myCamera;
UPROPERTY(EditDefaultsOnly, Category = "My Pawn|Vector")
FVector CameraLocation;
UPROPERTY(EditDefaultsOnly, Category = "My Pawn|Vector")
FRotator CameraRotation;
UPROPERTY(EditAnywhere, Category = "My Pawn|Vector")
float MaxSpeed;
FORCEINLINE UStaticMeshComponent* GetStaticMeshComponent() { return myStaticMesh; }
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void MoveRight(float Value);
void MoveForward(float Value);
void MouseLeft(float Value);
void MouseRight(float Value);
void MouseX(float Value);
void MouseY(float Value);
void MouseWheelAxis(float Value);
float VelocityRight;
float VelocityForward;
FVector Velocity;
FVector VelocityMouse;
FVector VelocityMouseForward;
float getMouseRight;
float getMouseLeft;
float getMouseX;
float getMouseY;
float getMouseWheelAxis;
float MouseMoveSpeed;
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
// 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;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
myStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("myStaticMesh"));
myStaticMesh->SetupAttachment(GetRootComponent());
static ConstructorHelpers::FObjectFinder<UStaticMesh>staticMeshAsset(TEXT("StaticMesh'/Game/s_Models/test1.test1'"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface>materialAsset(TEXT("Material'/Game/s_Models/Materials/black.black'"));
if (staticMeshAsset.Succeeded() && materialAsset.Succeeded())
{
myStaticMesh->SetStaticMesh(staticMeshAsset.Object);
myStaticMesh->SetMaterial(0, materialAsset.Object);
myStaticMesh->SetWorldScale3D(FVector(1.0f));
}
myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("myCamera"));
myCamera->SetupAttachment(GetStaticMeshComponent());
CameraLocation = FVector(-300.0f, 0.0f, 300.0f);
CameraRotation = FRotator(-45.0f, 0.0f, 0.0f);
myCamera->SetRelativeLocation(CameraLocation);
myCamera->SetRelativeRotation(CameraRotation);
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.0f;
VelocityRight = 0.0f;
VelocityForward = 0.0f;
Velocity = FVector(0.0f);
VelocityMouse = FVector(0.0f);
VelocityMouseForward = FVector(0.0f);
getMouseRight = 0.0f;
getMouseLeft = 0.0f;
getMouseX = 0.0f;
getMouseY = 0.0f;
getMouseWheelAxis = 0.0f;
MouseMoveSpeed = 0.0f;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MouseMoveSpeed = -FMath::Clamp(myCamera->GetRelativeLocation().X * 0.02f, -1000.0f, -10.0f);
Velocity = RootComponent->GetRightVector() * VelocityRight + RootComponent->GetForwardVector() * VelocityForward;
if (getMouseLeft == 1)
{
Velocity = RootComponent->GetRightVector() * -getMouseX * MaxSpeed * MouseMoveSpeed + RootComponent->GetForwardVector() * -getMouseY * MaxSpeed * MouseMoveSpeed;
AddActorWorldOffset(Velocity * DeltaTime, true);
}
if (getMouseRight == 1)
{
FRotator PitchRotation = myCamera->GetComponentRotation();
PitchRotation.Pitch = FMath::Clamp(PitchRotation.Pitch += getMouseY, -80.0F, 0.0F);
myCamera->SetWorldRotation(PitchRotation);
FRotator YawRotation = RootComponent->GetComponentRotation();
YawRotation.Yaw = YawRotation.Yaw += getMouseX;
RootComponent->SetWorldRotation(YawRotation);
}
if (getMouseWheelAxis != 0)
{
VelocityMouseForward.X = getMouseWheelAxis * MaxSpeed * MouseMoveSpeed;
myCamera->AddLocalOffset(VelocityMouseForward * DeltaTime, true);
}
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("s_MoveForward"), this, &AMyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("s_MoveRight"), this, &AMyPawn::MoveRight);
PlayerInputComponent->BindAxis(TEXT("s_MouseLeft"), this, &AMyPawn::MouseLeft);
PlayerInputComponent->BindAxis(TEXT("s_MouseRight"), this, &AMyPawn::MouseRight);
PlayerInputComponent->BindAxis(TEXT("s_MouseX"), this, &AMyPawn::MouseX);
PlayerInputComponent->BindAxis(TEXT("s_MouseY"), this, &AMyPawn::MouseY);
PlayerInputComponent->BindAxis(TEXT("s_MouseWheelAxis"), this, &AMyPawn::MouseWheelAxis);
}
void AMyPawn::MoveRight(float Value)
{
VelocityRight = FMath::Clamp(Value, -1.0f, 1.0f) * MaxSpeed;
}
void AMyPawn::MoveForward(float Value)
{
VelocityForward = FMath::Clamp(Value, -1.0f, 1.0f) * MaxSpeed;
}
void AMyPawn::MouseLeft(float Value)
{
getMouseLeft = Value;
}
void AMyPawn::MouseRight(float Value)
{
getMouseRight = Value;
}
void AMyPawn::MouseX(float Value)
{
getMouseX = FMath::Clamp(Value, -1.0f, 1.0f);
}
void AMyPawn::MouseY(float Value)
{
getMouseY = FMath::Clamp(Value, -1.0f, 1.0f);
}
void AMyPawn::MouseWheelAxis(float Value)
{
getMouseWheelAxis = FMath::Clamp(Value, -1.0f, 1.0f);
}