UEC++ 创建自定义类型案例

发布时间:2026/8/3 9:02:32

UEC++ 创建自定义类型案例 一子弹类型// .h文件 #include CoreMinimal.h #include GameFramework/Actor.h #include Components/SphereComponent.h #include GameFrameWork/ProjectileMovementComponent.h #include MyActor.generated.h // ... UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUStaticMeshComponent Sphere; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUSphereComponent SphereCollision; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUProjectileMovementComponent ProjectileMovement;// .cpp文件 AMyActor::AMyActor() { PrimaryActorTick.bCanEverTick true; Sphere CreateDefaultSubobjectUStaticMeshComponent(TEXT(Sphere)); SphereCollision CreateDefaultSubobjectUSphereComponent(TEXT(SphereCollision)); ProjectileMovement CreateDefaultSubobjectUProjectileMovementComponent(TEXT(ProjectileMovement)); static ConstructorHelpers::FObjectFinderUStaticMesh StaticSphereMesh(TEXT(/Engine/BasicShapes/Sphere.Sphere)); RootComponent Sphere; SphereCollision-SetupAttachment(RootComponent); ProjectileMovement-SetUpdatedComponent(RootComponent); Sphere-SetStaticMesh(StaticSphereMesh.Object); ProjectileMovement-InitialSpeed 500.0; ProjectileMovement-MaxSpeed 2000.0; }二带有增强输入的角色类型// .h文件 #include CoreMinimal.h #include InputActionValue.h #include EnhancedInputComponent.h #include EnhancedInputSubsystems.h #include GameFramework/Controller.h #include GameFramework/SpringArmComponent.h #include Camera/CameraComponent.h #include GameFramework/Character.h #include GameFramework/CharacterMovementComponent.h #include MyCharacter.generated.h class UInputAction; class UInputMappingContext; // ... public: UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUSpringArmComponent SpringArm; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUCameraComponent Camera; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputMappingContext DefaultMappingContext; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputAction MoveAction; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputAction LookAction; public: void Move(const FInputActionValue value); void Look(const FInputActionValue value); // ...// .cpp文件 AMyCharacter::AMyCharacter() { PrimaryActorTick.bCanEverTick true; SpringArm CreateDefaultSubobjectUSpringArmComponent(TEXT(SpringArm)); Camera CreateDefaultSubobjectUCameraComponent(TEXT(Camera)); SpringArm-TargetArmLength 400.0f; SpringArm-bUsePawnControlRotation true; SpringArm-SetupAttachment(RootComponent); Camera-SetupAttachment(SpringArm); //控制器只影响相机不影响角色 bUseControllerRotationPitch false; bUseControllerRotationRoll false; bUseControllerRotationYaw false; GetCharacterMovement()-bOrientRotationToMovement true; } void AMyCharacter::BeginPlay() { Super::BeginPlay(); if (auto PlayerController CastAPlayerController(Controller)) if (auto Subsystem ULocalPlayer::GetSubsystemUEnhancedInputLocalPlayerSubsystem(PlayerController-GetLocalPlayer())) Subsystem-AddMappingContext(DefaultMappingContext, 0); } void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); if (auto EnhancedInputComponent CastCheckedUEnhancedInputComponent(PlayerInputComponent)) { EnhancedInputComponent-BindAction(MoveAction, ETriggerEvent::Triggered, this, AMyCharacter::Move); EnhancedInputComponent-BindAction(LookAction, ETriggerEvent::Triggered, this, AMyCharacter::Look); } } void AMyCharacter::Move(const FInputActionValue value) { FVector2D MovementVector value.GetFVector2D(); if (Controller) { const FRotator Rotation Controller-GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); const FVector FowardDirection FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); const FVector RightDirection FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); AddMovementInput(FowardDirection, MovementVector.Y); AddMovementInput(RightDirection, MovementVector.X); } } // 5.7使用IMC_Default 和 IMC_MouseLook 将按键和鼠标操作分开 // IMC_Default → 按键操作移动、跳跃、冲刺、交互等 // IMC_MouseLook → 鼠标操作视角旋转 void AMyCharacter::Look(const FInputActionValue value) { FVector2D LookAxisVector value.GetFVector2D(); if (Controller) { AddControllerYawInput(LookAxisVector.X); AddControllerPitchInput(LookAxisVector.Y); } }

相关新闻