ue4c++日记6(操控一个character类 )

目录

操控一个character类 

头文件

cpp文件

操作映射

个人体会

1.角色移动

2.镜头上下左右移动


运行中显示碰撞

运行- 按“~”键,即打开控制台,输入show collision。

操控一个character类 

创建一个c++类,继承character-(公有)

头文件

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"

UCLASS()
class TTT_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMyCharacter();
	//摄像机组件
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
		class USpringArmComponent* SpringArmComp;//摇臂
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
		class UCameraComponent* Camera;//摄像机

	//角色属性
	float BaseTurnRate;//左右看速率
	float BaseLookupRate;//上下看速率
	void TurnAtRate(float Value);//左右看函数
	void TurnLookupRate(float Value);//上下看函数

	void MoveForward(float Value);//前后走函数
	void MoveRight(float Value);//左右走函数

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;

};

cpp文件

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


#include "MyCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "Engine/World.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	/// <summary>
	/// 摄像机
	/// </summary>
	SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArmComp->SetupAttachment(RootComponent);
	SpringArmComp->TargetArmLength = 600;
	SpringArmComp->bUsePawnControlRotation = true;

	Camera=CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
	//Camera->SetupAttachment(RootComponent);
	Camera->bUsePawnControlRotation = false;
	//一个true,一个false,指摄像机固定在摇臂上,
	//就可以同时控制两个组件,不用分开控制
	BaseTurnRate = 65.f;//左右看速率
	BaseLookupRate = 65.f;//上下看速率
	/// <summary>
	/// 3个false:控制器旋转时,模型不会跟着旋转
	/// </summary>
	bUseControllerRotationYaw = false;
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement()->bOrientRotationToMovement = true;//角色会朝着控制器选择的移动方向旋转
	GetCharacterMovement()->RotationRate = FRotator(0, 540, 0);//角色转身速度
	GetCharacterMovement()->JumpZVelocity = 650;//跳跃垂直加速度
	GetCharacterMovement()->AirControl = 0.2f;//空中横向运动控制量
}

void AMyCharacter::TurnAtRate(float Value)
{
	//GetWorld()->DeltaTimeSeconds	=	Tick
	AddControllerYawInput(Value * BaseTurnRate * GetWorld()->DeltaTimeSeconds);
}

void AMyCharacter::TurnLookupRate(float Value)
{
	AddControllerPitchInput(Value * BaseLookupRate * GetWorld()->DeltaTimeSeconds);
}

void AMyCharacter::MoveForward(float Value)
{
	if (Controller && Value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		//用控制器的左右旋转的值Rotation构造了一个旋转YawRotation
		//下面一句照抄就是,不懂
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

void AMyCharacter::MoveRight(float Value)
{
	if (Controller && Value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		//用控制器的左右旋转的值Rotation构造了一个旋转YawRotation
		//下面一句照抄就是,不懂
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}
}

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

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

}

// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	//绑定跳跃动作
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
	//向前后左右走
	PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
	//Play
	PlayerInputComponent->BindAxis("CameraPitch", this, &AMyCharacter::AddControllerPitchInput);
	PlayerInputComponent->BindAxis("CameraYaw", this, &AMyCharacter::AddControllerYawInput);
	//
	PlayerInputComponent->BindAxis("Lookup", this, &AMyCharacter::TurnLookupRate);
	PlayerInputComponent->BindAxis("Turn", this, &AMyCharacter::TurnAtRate);
}

操作映射

 


 

个人体会

1.角色移动

向前走为例

获取控制器的旋转,将其变成一个FRotator,放入旋转矩阵中,取出x方向。

addmovementinput(移动方向,鼠标位移);

将x方向,和UE4传值放入

void AMyCharacter::MoveForward(float Value)
{
	if (Controller && Value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

  

2.镜头上下左右移动

以Y轴为例:

左右转

 UE4传入值乘以转动基本值(自己定义的)*帧时间差

void AMyCharacter::TurnAtRate(float Value)
{
	//GetWorld()->DeltaTimeSeconds	=	Tick
	AddControllerYawInput(Value * BaseTurnRate * GetWorld()->DeltaTimeSeconds);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的UE4 C++代码示例,用于触发门的开启事件: 首先,创建一个继承自Actor的蓝图,将门模型拖放到该中,并为门添加一个Box组件作为门的触发器。 在.h文件中添加以下代码: ```c++ #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyDoor.generated.h" UCLASS() class MYPROJECT_API AMyDoor : public AActor { GENERATED_BODY() public: AMyDoor(); protected: UPROPERTY(VisibleAnywhere, Category = "Components") class UStaticMeshComponent* DoorMesh; UPROPERTY(VisibleAnywhere, Category = "Components") class UBoxComponent* TriggerBox; UPROPERTY(EditAnywhere, Category = "Properties") float OpenAngle = 90.0f; UPROPERTY(EditAnywhere, Category = "Properties") float DoorCloseDelay = 2.0f; float LastDoorOpenTime; UPROPERTY(EditAnywhere, Category = "Properties") float DoorOpenSpeed = 0.8f; UPROPERTY(EditAnywhere, Category = "Properties") float DoorCloseSpeed = 2.0f; UFUNCTION() void OnDoorOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); UFUNCTION() void OnDoorOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); public: virtual void Tick(float DeltaTime) override; }; ``` 在.cpp文件中添加以下代码: ```c++ #include "MyDoor.h" #include "Components/StaticMeshComponent.h" #include "Components/BoxComponent.h" AMyDoor::AMyDoor() { PrimaryActorTick.bCanEverTick = true; DoorMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorMesh")); RootComponent = DoorMesh; TriggerBox = CreateDefaultSubobject<UBoxComponent>(TEXT("TriggerBox")); TriggerBox->SetupAttachment(RootComponent); TriggerBox->OnComponentBeginOverlap.AddDynamic(this, &AMyDoor::OnDoorOverlapBegin); TriggerBox->OnComponentEndOverlap.AddDynamic(this, &AMyDoor::OnDoorOverlapEnd); LastDoorOpenTime = 0.0f; } void AMyDoor::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (GetWorld()->TimeSeconds - LastDoorOpenTime > DoorCloseDelay) { float CurrentYaw = DoorMesh->GetComponentRotation().Yaw; float TargetYaw; if (CurrentYaw > 0) { TargetYaw = 0.0f; } else { TargetYaw = OpenAngle; } float NewYaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, DoorCloseSpeed); DoorMesh->SetRelativeRotation(FRotator(0.0f, NewYaw, 0.0f)); } } void AMyDoor::OnDoorOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && OtherActor != this) { LastDoorOpenTime = GetWorld()->TimeSeconds; float CurrentYaw = DoorMesh->GetComponentRotation().Yaw; float NewYaw = FMath::FInterpTo(CurrentYaw, OpenAngle, GetWorld()->DeltaTimeSeconds, DoorOpenSpeed); DoorMesh->SetRelativeRotation(FRotator(0.0f, NewYaw, 0.0f)); } } void AMyDoor::OnDoorOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && OtherActor != this) { LastDoorOpenTime = GetWorld()->TimeSeconds; } } ``` 这个代码示例中,我们添加了一个门模型和一个Box组件用作门的触发器。在Box组件上注册了OnComponentBeginOverlap和OnComponentEndOverlap事件,当有物体进入或离开Box时,将触发相应的事件。在OnDoorOverlapBegin事件中,我们通过插值将门旋转到开启的角度,并在LastDoorOpenTime中存储当前时间。在Tick函数中,我们检查LastDoorOpenTime是否超过DoorCloseDelay,如果是,则插值将门旋转回关闭的角度。 最后,将蓝图添加到场景中,运行游戏并测试门的开启和关闭事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值