今天解决两大问题,第一个,键盘按键与鼠标轴向在UE中的绑定。第二个,解决UE中Charactor随着相机朝向的问题。
新建一个类继承与Charactor类.
在工程设置中找到Input.
并如下设置:
在头文件中写入以下代码:
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 #pragma once 4 5 #include "CoreMinimal.h" 6 #include "GameFramework/Character.h" 7 #include "MyCharacter.generated.h" 8 9 UCLASS() 10 class CHARACTOR_API AMyCharacter : public ACharacter 11 { 12 GENERATED_BODY() 13 14 public: 15 // Sets default values for this character's properties 16 AMyCharacter(); 17 18 protected: 19 // Called when the game starts or when spawned 20 virtual void BeginPlay() override; 21 22 public: 23 // Called every frame 24 virtual void Tick(float DeltaTime) override; 25 26 // Called to bind functionality to input 27 virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; 28 29 private: 30 FVector keyInput; 31 FRotator mouseInput; 32 float maxSpeed; 33 float mouseSpeed; 34 void forward(float value); 35 void right(float value); 36 void up(float value); 37 void yaw(float value); 38 void pitch(float value); 39 40 public: 41 UPROPERTY(VisibleAnywhere,BlueprintReadOnly) 42 class UStaticMeshComponent* myStaticMesh; 43 44 FORCEINLINE UStaticMeshComponent* GetmyStaticMesh() { 45 return myStaticMesh; 46 } 47 48 49 UPROPERTY(VisibleAnywhere, BlueprintReadOnly) 50 class USpringArmComponent* mySpringArm; 51 UPROPERTY(VisibleAnywhere, BlueprintReadOnly) 52 class UCameraComponent* myCamera; 53 };
在源文件中写入以下代码:
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 4 #include "MyCharacter.h" 5 #include "Components\InputComponent.h" 6 #include "Components\StaticMeshComponent.h" 7 #include "GameFramework\SpringArmComponent.h" 8 #include "Camera\CameraComponent.h" 9 #include "UObject\ConstructorHelpers.h" 10 11 12 // Sets default values 13 AMyCharacter::AMyCharacter() 14 { 15 // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. 16 PrimaryActorTick.bCanEverTick = true; 17 mouseInput = FRotator(0.0f); 18 keyInput = FVector::ZeroVector; 19 maxSpeed = 300.0f; 20 AutoPossessPlayer = EAutoReceiveInput::Player0; 21 mouseSpeed = 2.0f; 22 23 myStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("myStaticMesh")); 24 mySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("mySpringArm")); 25 myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("myCamera")); 26 27 auto myMeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/VREditor/TransformGizmo/SM_Sequencer_Key.SM_Sequencer_Key'")); 28 auto myMatAsset = ConstructorHelpers::FObjectFinder<UMaterialInterface>(TEXT("Material'/Engine/Functions/Engine_MaterialFunctions02/ExampleContent/GradientMap_Multi_Example.GradientMap_Multi_Example'")); 29 30 if (myMeshAsset.Succeeded() && myMatAsset.Succeeded()) { 31 myStaticMesh->SetStaticMesh(myMeshAsset.Object); 32 myStaticMesh->SetMaterial(0, myMatAsset.Object); 33 myStaticMesh->SetMaterial(1, myMatAsset.Object); 34 } 35 36 myStaticMesh->SetupAttachment(GetRootComponent()); 37 mySpringArm->SetupAttachment(GetmyStaticMesh()); 38 myCamera->SetupAttachment(mySpringArm); 39 40 mySpringArm->bEnableCameraLag = true; 41 mySpringArm->CameraLagSpeed = 30.0f; 42 mySpringArm->TargetArmLength = 300.0f; 43 44 45 myCamera->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f)); 46 myCamera->SetRelativeRotation(FRotator(-5.0f,0.0f,0.0f)); 47 48 //bUseControllerRotationYaw = true; 49 50 } 51 52 // Called when the game starts or when spawned 53 void AMyCharacter::BeginPlay() 54 { 55 Super::BeginPlay(); 56 57 } 58 59 // Called every frame 60 void AMyCharacter::Tick(float DeltaTime) 61 { 62 Super::Tick(DeltaTime); 63 64 65 auto direction_forward = myCamera->GetForwardVector(); 66 auto direction_right = myCamera->GetRightVector(); 67 68 69 if (keyInput.X == 1.0) { 70 keyInput = direction_forward; 71 keyInput *= maxSpeed; 72 }else if (keyInput.X == -1.0) { 73 keyInput = -direction_forward; 74 keyInput *= maxSpeed; 75 } 76 77 if (keyInput.Y == 1.0) { 78 keyInput = direction_right; 79 keyInput *= maxSpeed; 80 } 81 else if (keyInput.Y == -1.0) { 82 keyInput = -direction_right; 83 keyInput *= maxSpeed; 84 } 85 86 FHitResult hit; 87 AddActorLocalOffset(DeltaTime * keyInput, true, &hit); 88 89 FRotator Rotations = myStaticMesh->GetRelativeRotation(); 90 Rotations.Yaw += mouseInput.Yaw * mouseSpeed; 91 Rotations.Pitch = 0; 92 Rotations.Roll = 0; 93 myStaticMesh->SetRelativeRotation(Rotations); 94 95 96 //AddControllerYawInput(Rotations.Yaw); 97 98 99 FRotator CRotator = mySpringArm->GetComponentRotation(); 100 CRotator.Yaw = 0.0f; 101 CRotator.Pitch = FMath::Clamp(CRotator.Pitch + mouseInput.Pitch, -30.0f, 90.0f); 102 CRotator.Roll = 0.0f; 103 mySpringArm->SetRelativeRotation(CRotator); 104 105 } 106 107 // Called to bind functionality to input 108 void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) 109 { 110 Super::SetupPlayerInputComponent(PlayerInputComponent); 111 PlayerInputComponent->BindAxis("forward",this, &AMyCharacter::forward); 112 PlayerInputComponent->BindAxis("right",this, &AMyCharacter::right); 113 PlayerInputComponent->BindAxis("up",this, &AMyCharacter::up); 114 PlayerInputComponent->BindAxis("yaw",this, &AMyCharacter::yaw); 115 PlayerInputComponent->BindAxis("pitch",this, &AMyCharacter::pitch); 116 117 } 118 119 void AMyCharacter::forward(float value) 120 { 121 122 keyInput.X = FMath::Clamp(value, -1.0f, 1.0f); 123 } 124 125 void AMyCharacter::right(float value) 126 { 127 128 keyInput.Y = FMath::Clamp(value, -1.0f, 1.0f); 129 } 130 131 void AMyCharacter::up(float value) 132 { 133 keyInput.Z = FMath::Clamp(value, -1.0f, 1.0f) * maxSpeed; 134 } 135 136 void AMyCharacter::yaw(float value) 137 { 138 mouseInput.Yaw = FMath::Clamp(value, -1.0f, 1.0f) * mouseSpeed; 139 } 140 141 void AMyCharacter::pitch(float value) 142 { 143 mouseInput.Pitch = FMath::Clamp(value, -1.0f, 1.0f) * mouseSpeed; 144 }
这里需要注意将所有的物理属性关闭,重力属性关闭。
世界重力也要关闭。
效果如下,物体朝向与摄像机一致,按键方向也与朝向一致。