Hierarchy is intact
Used the first person template so just ignore the template code
header
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "mcCharacter.generated.h"
class UInputComponent;
class USkeletalMeshComponent;
class USceneComponent;
class UCameraComponent;
class UAnimMontage;
class USoundBase;
// Declaration of the delegate that will be called when the Primary Action is triggered
// It is declared as dynamic so it can be accessed also in Blueprints
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnUseItem);
UCLASS(config=Game)
class AmcCharacter : public ACharacter
{
GENERATED_BODY()
/** Pawn mesh: 1st person view (arms; seen only by self) */
UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
USkeletalMeshComponent* Mesh1P;
/** First person camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
UCameraComponent* FirstPersonCameraComponent;
public:
AmcCharacter();
protected:
virtual void BeginPlay();
public:
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float TurnRateGamepad;
/** Delegate to whom anyone can subscribe to receive this event */
UPROPERTY(BlueprintAssignable, Category = "Interaction")
FOnUseItem OnUseItem;
protected:
/** Fires a projectile. */
void OnPrimaryAction();
/** Handles moving forward/backward */
void MoveForward(float Val);
/** Handles strafing movement, left and right */
void MoveRight(float Val);
/**
* Called via input to turn at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void TurnAtRate(float Rate);
/**
* Called via input to turn look up/down at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void LookUpAtRate(float Rate);
struct TouchData
{
TouchData() { bIsPressed = false;Location=FVector::ZeroVector;}
bool bIsPressed;
ETouchIndex::Type FingerIndex;
FVector Location;
bool bMoved;
};
void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location);
void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location);
void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location);
TouchData TouchItem;
protected:
// APawn interface
virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;
// End of APawn interface
/*
* Configures input for touchscreen devices if there is a valid touch interface for doing so
*
* @param InputComponent The input component pointer to bind controls to
* @returns true if touch controls were enabled.
*/
bool EnableTouchscreenMovement(UInputComponent* InputComponent);
public:
/** Returns Mesh1P subobject **/
USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
/** Returns FirstPersonCameraComponent subobject **/
UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; }
UPROPERTY(BlueprintReadWrite, editAnywhere)
USkeletalMeshComponent* mccHandMesh_L;
UPROPERTY(BlueprintReadWrite, editAnywhere)
UStaticMesh* StaticMeshLaser;
UPROPERTY(BlueprintReadWrite, editAnywhere)
UStaticMeshComponent* laserMesh_L;
UPROPERTY(BlueprintReadWrite, editAnywhere)
UStaticMeshComponent* laserHitMesh_L;
UPROPERTY(BlueprintReadWrite, editAnywhere)
UStaticMesh* StaticMeshLaserHit;
UPROPERTY(BlueprintReadWrite, editAnywhere)
float LASER_HAND_DISTANCE = 200;
UPROPERTY(BlueprintReadWrite,editAnywhere)
USkeletalMesh* SkeletalMeshHand_L;
UPROPERTY(BlueprintReadWrite, editAnywhere)
class Ugen_MotionControllerComponent* mccHand_L;
};
cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "mcCharacter.h"
#include "mcProjectile.h"
#include "Animation/AnimInstance.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/InputSettings.h"
#include "gen_MotionControllerComponent.h"
//////////////////////////////////////////////////////////////////////////
// AmcCharacter
AmcCharacter::AmcCharacter()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
// set our turn rates for input
TurnRateGamepad = 45.f;
// Create a CameraComponent
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
FirstPersonCameraComponent->SetRelativeLocation(FVector(-39.56f, 1.75f, 64.f)); // Position the camera
FirstPersonCameraComponent->bUsePawnControlRotation = true;
// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
Mesh1P->SetOnlyOwnerSee(true);
Mesh1P->SetupAttachment(FirstPersonCameraComponent);
Mesh1P->bCastDynamicShadow = false;
Mesh1P->CastShadow = false;
Mesh1P->SetRelativeRotation(FRotator(1.9f, -19.19f, 5.2f));
Mesh1P->SetRelativeLocation(FVector(-0.5f, -4.4f, -155.7f));
////////////////////////////////////////////
// LEFT HAND
////////////////////////////////////////////
// motion controller
mccHand_L = CreateDefaultSubobject<Ugen_MotionControllerComponent>(TEXT("mccHand_L"));
mccHand_L->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
mccHand_L->SetTrackingSource(EControllerHand::Left);
// hand skeletal mesh
mccHandMesh_L = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("mccHandMesh_L"));
mccHandMesh_L->AttachToComponent(mccHand_L, FAttachmentTransformRules::KeepRelativeTransform);
mccHandMesh_L->SetRelativeLocation(FVector::ZeroVector);
mccHandMesh_L->SetRelativeRotation(FRotator(-90.f, 90.f, 0));
//ConstructorHelpers::FObjectFinder SkeletalMeshHand_L(TEXT(" / Script / Engine.SkeletalMesh’ / gen_Game / HTAB / meshes / translucent_hands / TranslucentHand_L_UE4.TranslucentHand_L_UE4’"));
mccHandMesh_L->SetSkeletalMesh(SkeletalMeshHand_L);
// laser
laserMesh_L = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("laserMesh_L"));
laserMesh_L->SetStaticMesh(StaticMeshLaser);
laserMesh_L->AttachToComponent(mccHandMesh_L, FAttachmentTransformRules::KeepRelativeTransform);
laserMesh_L->SetRelativeLocation(FVector(LASER_HAND_DISTANCE, 0.f, 0.f));
laserMesh_L->SetRelativeRotation(FRotator(-90.f, 0.f, 0.f));
// laser hit
laserHitMesh_L = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("laserHitMesh_L"));
laserHitMesh_L->SetStaticMesh(StaticMeshLaserHit);
laserHitMesh_L->SetWorldScale3D(FVector(0.02f, 0.02f, 0.02f));
}
void AmcCharacter::BeginPlay()
{
// Call the base class
Super::BeginPlay();
}
//////////////////////////////////////////////////////////////////////////// Input
void AmcCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
// Bind jump events
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
// Bind fire event
PlayerInputComponent->BindAction("PrimaryAction", IE_Pressed, this, &AmcCharacter::OnPrimaryAction);
// Enable touchscreen input
EnableTouchscreenMovement(PlayerInputComponent);
// Bind movement events
PlayerInputComponent->BindAxis("Move Forward / Backward", this, &AmcCharacter::MoveForward);
PlayerInputComponent->BindAxis("Move Right / Left", this, &AmcCharacter::MoveRight);
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "Mouse" versions handle devices that provide an absolute delta, such as a mouse.
// "Gamepad" versions are for devices that we choose to treat as a rate of change, such as an analog joystick
PlayerInputComponent->BindAxis("Turn Right / Left Mouse", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("Look Up / Down Mouse", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Turn Right / Left Gamepad", this, &AmcCharacter::TurnAtRate);
PlayerInputComponent->BindAxis("Look Up / Down Gamepad", this, &AmcCharacter::LookUpAtRate);
}
void AmcCharacter::OnPrimaryAction()
{
// Trigger the OnItemUsed Event
OnUseItem.Broadcast();
}
void AmcCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
if (TouchItem.bIsPressed == true)
{
return;
}
if ((FingerIndex == TouchItem.FingerIndex) && (TouchItem.bMoved == false))
{
OnPrimaryAction();
}
TouchItem.bIsPressed = true;
TouchItem.FingerIndex = FingerIndex;
TouchItem.Location = Location;
TouchItem.bMoved = false;
}
void AmcCharacter::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
if (TouchItem.bIsPressed == false)
{
return;
}
TouchItem.bIsPressed = false;
}
void AmcCharacter::MoveForward(float Value)
{
if (Value != 0.0f)
{
// add movement in that direction
AddMovementInput(GetActorForwardVector(), Value);
}
}
void AmcCharacter::MoveRight(float Value)
{
if (Value != 0.0f)
{
// add movement in that direction
AddMovementInput(GetActorRightVector(), Value);
}
}
void AmcCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}
void AmcCharacter::LookUpAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerPitchInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}
bool AmcCharacter::EnableTouchscreenMovement(class UInputComponent* PlayerInputComponent)
{
if (FPlatformMisc::SupportsTouchInput() || GetDefault<UInputSettings>()->bUseMouseForTouch)
{
PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AmcCharacter::BeginTouch);
PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &AmcCharacter::EndTouch);
return true;
}
return false;
}