按键【W】【S】【A】【D】实现上下左右移动,【空格】跳跃
移动鼠标实现方向的移动
- 设置按键映射

- 代码
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MainCharacter.generated.h"
UCLASS()
class FIRSTPROJECT_API AMainCharacter : public ACharacter {
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMainCharacter();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* cameraSpringArm;
FORCEINLINE class USpringArmComponent* getCameraSpringArm() {
return cameraSpringArm;
}
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* followCamera;
FORCEINLINE UCameraComponent* getFollowCamera() {
return followCamera;
}
//使用方向键的转动速率
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
float baseTurnRate;//度每秒
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
float baseLookupRate;
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;
private:
void moveForward(float value);
void moveRight(float value);
void turnAtRate(float rate);
void lookUpAtRate(float tate);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
// Sets default values
AMainCharacter::AMainCharacter() {
// 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;
cameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
cameraSpringArm->SetupAttachment(GetRootComponent());
cameraSpringArm->TargetArmLength = 400.0;
cameraSpringArm->bUsePawnControlRotation = true; //摇臂跟着控制器的旋转
//设置碰撞胶囊的大小
GetCapsuleComponent()->SetCapsuleSize(48, 105.0);
followCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
followCamera->SetupAttachment(cameraSpringArm, USpringArmComponent::SocketName);
followCamera->bUsePawnControlRotation = false; //摄像机不跟着摇臂旋转
//当摄像机旋转的时候,角色不会旋转,仅对摄像机有影响 (上下左右按键)
bUseControllerRotationYaw = false;
//将选装绑定到角色上,而不是绑定到控制器上
GetCharacterMovement()->bOrientRotationToMovement = true;//角色沿着基于输入的方向移动
GetCharacterMovement()->RotationRate = FRotator(0.0, 540.0, 0.0);
GetCharacterMovement()->JumpZVelocity = 650.0;//向上跳起的高度和速度
GetCharacterMovement()->AirControl = 0.2;//当角色跳起的时候,可以在空中进行控制,如果不想被控制的话,可以设置一个比较小的值
}
// Called when the game starts or when spawned
void AMainCharacter::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void AMainCharacter::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAxis("MoveForward", this, &AMainCharacter::moveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMainCharacter::moveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("Lookup", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("TurnRate", this, &AMainCharacter::turnAtRate);
PlayerInputComponent->BindAxis("LookupRate", this, &AMainCharacter::lookUpAtRate);
}
void AMainCharacter::moveForward(float value) {
if ((Controller!=nullptr) && (value != 0.0)){
FRotator rotation = Controller->GetControlRotation();
FRotator yawRotation(0.0, rotation.Yaw, 0.0);
FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(direction, value);
}
}
void AMainCharacter::moveRight(float value) {
if ((Controller != nullptr) && (value != 0.0)) {
FRotator rotation = Controller->GetControlRotation();
FRotator yawRotation(0.0, rotation.Yaw, 0.0);
FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(direction, value);
}
}
void AMainCharacter::turnAtRate(float rate) {
AddControllerYawInput(rate * baseTurnRate * GetWorld()->GetDeltaSeconds());
}
void AMainCharacter::lookUpAtRate(float rate) {
AddControllerPitchInput(rate * baseLookupRate * GetWorld()->GetDeltaSeconds());
}
- 在世界场景中设置游戏模式

本文介绍了一个游戏主角色的控制实现方式,包括使用WASD键进行移动、空格键跳跃等功能,并详细展示了如何通过代码设置玩家输入与角色动作之间的映射。

617

被折叠的 条评论
为什么被折叠?



