I have a problem when creating an action mappin. I created an action mappin named Actions and linked it to the F key but the action I want to perform does not work with this key but if I change the F to the E key for example the action is performed.
Code where I apply it
#pragma once
include “CoreMinimal.h”
include “GameFramework/Character.h”
include “Weapon.h”
include “PlayerCharacter1.generated.h”
UCLASS()
class PRUEBAUE1_API APlayerCharacter1 : public ACharacter
{
GENERATED_BODY()
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = “true”))
class USpringArmComponent* CameraBoom;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
float BaseTurnRate;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
float BaseLookUpRate;
UPROPERTY()
AWeapon* EquippedWeapon;
UPROPERTY()
AWeapon* FocusedWeapon;
float DefaultWalkSpeed;
float SprintSpeed;
float CrouchSpeed;
bool ActionPressed = false;
public:
APlayerCharacter1();
protected:
virtual void BeginPlay() override;
void Run();
void StopRunning();
void Actions();
void OnActionPressed();
void OnActionReleased();
void Crouch();
void StopCrouch();
void MoveForward(float Value);
void MoveRight(float Value);
void TurnAtRate(float Rate);
void LookAtRate(float Rate);
void EquipWeapon(AWeapon* Weapon);
void UpdateFocusedWeapon();
bool TraceUnderCrosshairs(FHitResult& OutHitResult);
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};
PlayerCharacter1.h
void APlayerCharacter1::Actions()
{
UE_LOG(LogTemp, Warning, TEXT(“F key pressed”));
if (FocusedWeapon)
{
UE_LOG(LogTemp, Warning, TEXT("Recogiendo arma: %s"), *FocusedWeapon->GetName());
EquipWeapon(FocusedWeapon);
FocusedWeapon = nullptr; // Limpiar la referencia al arma enfocada después de equiparla
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No hay arma enfocada para recoger"));
}
}
void APlayerCharacter1::OnActionPressed()
{
ActionPressed = true;
UE_LOG(LogTemp, Warning, TEXT(“OnActionPressed: ActionPressed set to true”));
Actions(); // Llama a Actions cuando la tecla es presionada
}
void APlayerCharacter1::OnActionReleased()
{
ActionPressed = false;
UE_LOG(LogTemp, Warning, TEXT(“OnActionReleased: ActionPressed set to false”));
}
void APlayerCharacter1::EquipWeapon(AWeapon* Weapon)
{
if (Weapon)
{
UE_LOG(LogTemp, Warning, TEXT(“Equipando arma: %s”), *Weapon->GetName());
// Determina qué socket usar según el tipo de arma
FName SocketName;
if (Weapon->IsA<ARaygun>())
{
SocketName = "WeaponSocket_RG";
USkeletalMeshComponent* CharacterMesh = GetMesh();
if (CharacterMesh) {
Weapon->AttachToComponent(CharacterMesh, FAttachmentTransformRules::SnapToTargetIncludingScale, SocketName);
Weapon->SetActorHiddenInGame(true); //ocultar el arma en el mundo
Weapon->SetActorEnableCollision(false); // Desactivar colisiones del arma
EquippedWeapon = Weapon; //Guardar referencia al arma equipada
UE_LOG(LogTemp, Warning, TEXT("Arma equipada: %s"), *Weapon->GetName());
}
else
{
UE_LOG(LogTemp, Error, TEXT("No se encontró el mesh del personaje"));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No se encontró el arma para equipar"));
}
}
}
void APlayerCharacter1::UpdateFocusedWeapon()
{
FHitResult HitResult;
if (TraceUnderCrosshairs(HitResult))
{
AWeapon* HitWeapon = Cast(HitResult.GetActor());
if (HitWeapon && HitWeapon != EquippedWeapon)
{
FocusedWeapon = HitWeapon;
UE_LOG(LogTemp, Warning, TEXT(“Arma enfocada: %s”), *FocusedWeapon->GetName());
if (ActionPressed) {
EquipWeapon(FocusedWeapon);
}
}
else
{
FocusedWeapon = nullptr;
UE_LOG(LogTemp, Warning, TEXT("El trazo no golpeó un arma"));
}
}
else
{
FocusedWeapon = nullptr;
UE_LOG(LogTemp, Warning, TEXT("El trazo no golpeó nada"));
}
}
bool APlayerCharacter1::TraceUnderCrosshairs(FHitResult& OutHitResult)
{
FVector Start = FollowCamera->GetComponentLocation();
FVector ForwardVector = FollowCamera->GetForwardVector();
Start = Start + (ForwardVector * 350.0f);
FVector End = Start + (ForwardVector * 200.0f); // Aumenta la distancia de detección si es necesario
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
// Solo para depuración, dibuja la línea de trazo
DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 1, 0, 1);
return GetWorld()->LineTraceSingleByChannel(OutHitResult, Start, End, ECC_Visibility, Params);
}
void APlayerCharacter1::Run()
{
GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
}
void APlayerCharacter1::StopRunning()
{
GetCharacterMovement()->MaxWalkSpeed = DefaultWalkSpeed;
}
void APlayerCharacter1::TurnAtRate(float Rate)
{
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}
void APlayerCharacter1::LookAtRate(float Rate)
{
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
}
// Called every frame
void APlayerCharacter1::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdateFocusedWeapon();
}
// Called to bind functionality to input
void APlayerCharacter1::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
//Movimiento de personaje
PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter1::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter1::MoveRight);
//Movimiento de camara botones
PlayerInputComponent->BindAxis("TurnRate", this, &APlayerCharacter1::TurnAtRate);
PlayerInputComponent->BindAxis("LookUpRate", this, &APlayerCharacter1::LookAtRate);
//Movimiento de camara raton
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
//Salto y Desalto
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
//Correr
PlayerInputComponent->BindAction("Run", IE_Pressed, this, &APlayerCharacter1::Run);
PlayerInputComponent->BindAction("Run", IE_Released, this, &APlayerCharacter1::StopRunning);
//Agacharse
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &APlayerCharacter1::Crouch);
PlayerInputComponent->BindAction("Crouch", IE_Released, this, &APlayerCharacter1::StopCrouch);
//Recoger Arma
//PlayerInputComponent->BindAction("Actions", IE_Pressed, this, &APlayerCharacter1::Actions);
PlayerInputComponent->BindAction("Actions", IE_Pressed, this, &APlayerCharacter1::OnActionPressed);
PlayerInputComponent->BindAction("Actions", IE_Released, this, &APlayerCharacter1::OnActionReleased);
UE_LOG(LogTemp, Warning, TEXT("Input setup completed"));
}
PlayerCharacter1.cpp
Mi codigo esta correcto hasta ahi, creo pero ese no es el punto. Cuando el trazado esta detectando el arma que quiero agarrar y presiono la F este nisiquiera da un mensaje en consola y obviamente lo configure 1000 veces en el input de mi ide, pero si cambio la tecla F a la tecla E por ejemplo si funciona, que alguien me ayude porfavor, soy noob