“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Under the source code, no secrets. – Hou Jie
1. Create a C++ project
- Create a C++ class that inherits from
ACharacter
, namedThirdCharacter
2. Thirdcharacter.h analysis
- Create the camera arm component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* ArmCamera;
Copy the code
Can only be read VisibleAnywhere, BlueprintReadOnly blueprint. A type of Camera
- Creating a Camera Component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent *FallowCamera;
Copy the code
- Forward and backward function
UFUNCTION(BlueprintCallable)
void MoveForward(float Value);
Copy the code
- Left and right shift function
UFUNCTION(BlueprintCallable)
void MoveRight(float Value);
Copy the code
3. Thirdcharacter.cpp analysis
- Set initial radius of capsule to height
GetCapsuleComponent() - >SetCapsuleSize(Radius, Height);
Copy the code
Note that the default collision judgment is capsule
- Don’t let the input controller control the capsule’s Rotation. Let the input control only affect the camera
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
Copy the code
-
Set character movement
-
Rotate the character in the specified direction before moving
GetCharacterMovement()->bOrientRotationToMovement = true; Copy the code
-
Set the capsule rotation speed
GetCharacterMovement()->RotationRate = FRotator(0.0 f.540.0 f.0.0 f); Copy the code
-
Jump z-axis height
GetCharacterMovement()->JumpZVelocity = 450.f; Copy the code
-
Air control, the amount of lateral movement control available to the character when falling. 0 = no control, 1 = full control at MaxWalkSpeed’s maximum speed.
GetCharacterMovement()->AirControl = 0.2 f; Copy the code
-
-
Create a camera arm (if there is a collision, pull it towards the player)
ArmCamera = CreateDefaultSubobject<USpringArmComponent>(TEXT("ArmCamera")); ArmCamera->SetupAttachment(RootComponent); Copy the code
-
Set the distance the camera is behind the character
ArmCamera->TargetArmLength = 300.0 f; Copy the code
-
The camera arm rotates based on the controller
ArmCamera->bUsePawnControlRotation = true; Copy the code
-
Create a camera
FallowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
Copy the code
-
Attach the camera to the end of the boom
FallowCamera->SetupAttachment(ArmCamera, USpringArmComponent::SocketName); Copy the code
-
The camera does not rotate relative to the arm
FallowCamera->bUsePawnControlRotation = false; Copy the code
-
Basic operations such as binding keys
-
Jump,IE_Pressed bound to ACharacter::Jump,IE_Released bound to ACharacter::StopJumping
-
MoveForward, bound to the AThirdCharacter: : MoveForward
-
MoveRight, bound to the AThirdCharacter: : MoveRight
-
Turn, is bound to the APawn: : AddControllerYawInput, AThirdCharacter inheritance in ACharacter, ACharacter to APawn inheritance
-
LookUp, bound to the APawn: : AddControllerPitchInput, AThirdCharacter inheritance in ACharacter, ACharacter to APawn inheritance
-
MoveForward function, I feel the function is to get the current camera Angle, to rotate the X-axis of the character to the current Angle of view X-axis
void AThirdCharacter::MoveForward(float Value)
{
const FRotator Rotation = Controller->GetControlRotation(a);const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);// I feel that the function is to get the current camera Angle,
// Rotate the X-axis of the character to match the X-axis of the current viewing Angle
AddMovementInput(Direction, Value);
}
Copy the code
Can you elaborate on the function FRotationMatrix? Welcome to comment
4. Bind keyboard to UE4
5. Set the static mesh of the capsule character skeleton and set the animation
Material for the parakong material, animation blueprint for their own animation design according to the material