UE4的InputComponent无法使用
cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyCharacter.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;
}
// 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);
InputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
InputComponent->BindAxis("MoveBack", this, &AMyCharacter::MoveBack);
InputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
InputComponent->BindAxis("MoveLeft", this, &AMyCharacter::MoveLeft);
InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
void AMyCharacter::MoveForward(float val)
{
AddMovementInput(GetActorForwardVector(), val);
}
void AMyCharacter::MoveBack(float val)
{
AddMovementInput(-GetActorForwardVector(), val);
}
void AMyCharacter::MoveRight(float val)
{
AddMovementInput(GetActorRightVector(), val);
}
void AMyCharacter::MoveLeft(float val)
{
AddMovementInput(-GetActorRightVector(), val);
}
h:
// 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 GAMEPROJECT_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
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 val);//人物往前移动
void MoveBack(float val); //人物向后
void MoveRight(float val); //人物向右
void MoveLeft(float val); //人物向左
};
在InputComponent的地方有红线,但是可以成功编译,编译过后那段代码不起作用(无法移动和旋转视角)
希望可以把character移动和旋转视角,让InputComponent有效