How can I get the input from the mouse?

I’ve tried using

Controller->InputComponent->GetAxisKeyValue(“MoveRight”);

I’m trying to get the float value the current mouse horizontal movement is giving.

1 Like

Hi ,

You can try it this way:

First, setup your Axis Mapping in the Editor.

Once that is done, go into your character.h file and make sure you declare a function to handle whatever you want to do with the input and an override for the SetupPlayerInputComponent function…

// character.h

protected:
	void TurnFunction(float Value);
	
protected:
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) OVERRIDE;

In your character.cpp use SetupPlayerInputComponent to bind your input and define what you want to happen with the input inside the function you declared previously…

// Character.cpp

void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	check(InputComponent);

	InputComponent->BindAxis("MouseMovement", this, &AMyCharacter::TurnFunction);
}

void AMyCharacter::TurnFunction(float Value)
{
	if (Value != 0.0f)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(FString::SanitizeFloat(Value)));
		}
	}

	AddControllerYawInput(Value);
}

The code above will output an onscreen debug message whenever the mouse’s X value has changed since the last tick, then rotate the camera that amount (this is based on the basic code template project).

You don’t have to have your character class handle the input, but that might be the easiest option.