目录
运行中显示碰撞
运行- 按“~”键,即打开控制台,输入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);
}