I am new at unreal engine and for our project I need write a script to rotate 4 objects when I press 'A' or 'D'. Let's say they're wheels. I just applied some code from a tutorial but I can't do what I wanna do with it. When I drag and drop into an object just anything happens . Is it possible writing a script that I can just drag and drop it into an object in the scene and when I press A or D it will just rotate it? My code is like that;
header file:
cpp file:
When I drag and drop the script into a cube hierarchy is just like that.
header file:
Code:
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Pawn.h" #include "MyPawn.generated.h" UCLASS() class MYPROJECT_API AMyPawn : public APawn { GENERATED_BODY() public: // Sets default values for this pawn's properties AMyPawn(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(EditAnywhere) USceneComponent* OurVisibleComponent; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; void RotatePositive(); void StopRotatingPositive(); void RotateNegative(); void StopRotatingNegative(); bool rotatePositive, rotateNegative; };
Code:
// Fill out your copyright notice in the Description page of Project Settings. #include "MyPawn.h" #include "Components/InputComponent.h" // Sets default values AMyPawn::AMyPawn() { // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; AutoPossessPlayer = EAutoReceiveInput::Player0; } // Called when the game starts or when spawned void AMyPawn::BeginPlay() { Super::BeginPlay(); } // Called every frame void AMyPawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); { if (rotatePositive) { FQuat QuatRotation = FQuat(FRotator(1, 0, 0)); AddActorLocalRotation(QuatRotation, false, 0, ETeleportType::None); } if (rotateNegative) { FQuat QuatRotation = FQuat(FRotator(-1, 0, 0)); AddActorLocalRotation(QuatRotation, false, 0, ETeleportType::None); } } } // Called to bind functionality to input void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); InputComponent->BindAction("rotatePositive",IE_Pressed, this, &AMyPawn::RotatePositive); InputComponent->BindAction("rotatePositive", IE_Released, this, &AMyPawn::StopRotatingPositive); InputComponent->BindAction("rotateNegative", IE_Pressed, this, &AMyPawn::RotateNegative); InputComponent->BindAction("rotateNegative", IE_Released, this, &AMyPawn::StopRotatingNegative); } void AMyPawn::RotatePositive() { rotatePositive = true; } void AMyPawn::RotateNegative() { rotateNegative = true; } void AMyPawn::StopRotatingPositive() { rotatePositive = false; } void AMyPawn::StopRotatingNegative() { rotateNegative = false; }
Comment