当前位置:   article > 正文

UE4 C++ Character的移动和摇头_uec++动态生成的character无法move

uec++动态生成的character无法move

UE4 C++ Character的移动和摇头

目前知道两种方法:
看注释

.h:

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

#pragma once

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

UCLASS()
class GETSTARTED_API AMyinPlayer : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMyinPlayer();

	UPROPERTY(VisibleAnywhere,BlueprintReadOnly)
		class USpringArmComponent* SpringArm;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
		class UCameraComponent* FollowCamera;

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 MoveForward(float value);
	void MoveRight(float value);
};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

.cpp:

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


#include "Characters/Player/MyinPlayer.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AMyinPlayer::AMyinPlayer()
{
 	// 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;

	SpringArm=CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArm->SetupAttachment(GetRootComponent());

	SpringArm->TargetArmLength = 600.0f;
	SpringArm->bUsePawnControlRotation = true;

	FollowCamera=CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
	FollowCamera->bUsePawnControlRotation = false;
	GetCapsuleComponent()->SetCapsuleSize(25.0f, 100.0f);//设置胶囊体组件的高和宽

	bUseControllerRotationYaw = false;

	GetCharacterMovement()->bOrientRotationToMovement = true;//继承角色移动类里面的“将旋转朝向远动”设置为
	GetCharacterMovement()->RotationRate=FRotator(0.0f, 500.0f, 0.0f);//继上面“旋转速率”

}

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

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

}

// Called to bind functionality to input
void AMyinPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	check(PlayerInputComponent);//是否有效,可用性的检查

	PlayerInputComponent->BindAxis("MoveForward", this, &AMyinPlayer::MoveForward);//第一个是轴映射的名称
	PlayerInputComponent->BindAxis("MoveRight", this, &AMyinPlayer::MoveRight);

	PlayerInputComponent->BindAxis("Turn", this, &AMyinPlayer::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &AMyinPlayer::AddControllerPitchInput);
}

void AMyinPlayer::MoveForward(float value)
{
	//if ((Controller != nullptr) && (value != 0.0f)) {
		//FRotator Rotation = Controller->GetControlRotation();
		//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
		//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		//AddMovementInput(Direction, value);
	//}

	AddMovementInput(FollowCamera->GetForwardVector(), value);//添加移动输入
}

void AMyinPlayer::MoveRight(float value)
{
	//if ((Controller != nullptr) && (value != 0.0f)) {
		//FRotator Rotation = Controller->GetControlRotation();
		//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
		//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		//AddMovementInput(Direction, value);
	//}
	AddMovementInput(FollowCamera->GetRightVector(), value);
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

C++设置的硬设置:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/133359
推荐阅读
相关标签
  

闽ICP备14008679号