Hi,
i’m trying to achieve a crouch function for my character but i just can’t get it to work.
I have a character with a custom charactermovement and successfully implemented a jump-function and some debugmessages
to confirm the use of them.
When i’m setting the “bWantsToCrouch” in charactermovement it switches from “false” to “true” on the debugmessage
(displayall charactermovementcomponent bwantstocrouch) but the collisionsphyl doesn’t change it’s size nor does my cameraheight.
When i’m calling “crouch()” it doesn’t do anything. Even the “bWantsToCrouch” flag remains “false”.
???
(btw UE4.3)
TFP_Character.h :
#pragma once
#include "TFP_CharacterMovementComponent.h"
#include "GameFramework/Character.h"
#include "TFP_Character.generated.h"
/**
*
*/
UCLASS()
class TEST_FIRSTPERSON_API ATFP_Character : public ACharacter
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() OVERRIDE;
void CheckJumpInput(float DeltaTime) OVERRIDE;
void StopJumping() OVERRIDE;
UPROPERTY()
float JumpPower;
protected:
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) OVERRIDE;
UFUNCTION()
void MoveForward(float Val);
UFUNCTION()
void MoveRight(float Val);
UFUNCTION()
void StartCrouch();
UFUNCTION()
void EndCrouch();
};
TFP_Character.cpp :
#include "TEST_FirstPerson.h"
#include "TFP_Character.h"
ATFP_Character::ATFP_Character(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP.SetDefaultSubobjectClass<UTFP_CharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
JumpKeyHoldTime = 0.0f;
JumpMaxHoldTime = 5.0f;
JumpPower = 0.0f;
}
void ATFP_Character::BeginPlay(){
Super::BeginPlay();
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("TFP_Character !"));
}
void ATFP_Character::SetupPlayerInputComponent(UInputComponent* InputComponent){
InputComponent->BindAxis("MoveForward", this, &ATFP_Character::MoveForward);
InputComponent->BindAxis("MoveRight", this, &ATFP_Character::MoveRight);
InputComponent->BindAxis("Turn", this, &ATFP_Character::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &ATFP_Character::AddControllerPitchInput);
InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
InputComponent->BindAction("Duck", IE_Pressed, this, &ATFP_Character::StartCrouch);
InputComponent->BindAction("Duck", IE_Released, this, &ATFP_Character::EndCrouch);
}
void ATFP_Character::MoveForward(float Value){
if ((Controller != NULL) && (Value != 0.0f)){
// find out which way is forward
FRotator Rotation = Controller->GetControlRotation();
// Limit pitch when walking or falling
if (CharacterMovement->IsMovingOnGround() || CharacterMovement->IsFalling())
Rotation.Pitch = 0.0f;
// add movement in that direction
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
void ATFP_Character::MoveRight(float Value){
if ((Controller != NULL) && (Value != 0.0f)){
// find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
// add movement in that direction
AddMovementInput(Direction, Value);
}
}
void ATFP_Character::CheckJumpInput(float DeltaTime){
//const bool bWasJumping = bPressedJump && JumpKeyHoldTime > 0.0f;
if (bPressedJump){
JumpKeyHoldTime += DeltaTime;
//const bool bDidJump = DoJump(bClientUpdating);
//if (!bWasJumping && bDidJump)
//OnJumped();
JumpPower += JumpKeyHoldTime / 5;
}
}
void ATFP_Character::StopJumping(){
Super::StopJumping();
DoJump(bClientUpdating);
JumpPower = 0;
}
void ATFP_Character::StartCrouch(){
//if (CharacterMovement->IsMovingOnGround())
Crouch();
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("crouch"));
//CharacterMovement->bWantsToCrouch = true;
}
void ATFP_Character::EndCrouch(){
//if (CharacterMovement->IsMovingOnGround())
UnCrouch();
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("uncrouch"));
//CharacterMovement->bWantsToCrouch = false;
}
TFP_CharacterMovementComponent.h :
#pragma once
#include "GameFramework/CharacterMovementComponent.h"
#include "TFP_CharacterMovementComponent.generated.h"
/**
*
*/
UCLASS()
class TEST_FIRSTPERSON_API UTFP_CharacterMovementComponent : public UCharacterMovementComponent
{
GENERATED_UCLASS_BODY()
virtual bool DoJump() override;
protected:
virtual void InitializeComponent() override;
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
};
TFP_CharacterMovementComponent.cpp :
#include "TEST_FirstPerson.h"
#include "TFP_CharacterMovementComponent.h"
//#include "TFP_GameMode.h"
#include "TFP_Character.h"
UTFP_CharacterMovementComponent::UTFP_CharacterMovementComponent(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("TFP_CharacterMovementComponent !"));
}
void UTFP_CharacterMovementComponent::InitializeComponent(){
Super::InitializeComponent();
}
void UTFP_CharacterMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType,
FActorComponentTickFunction *ThisTickFunction){
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
bool UTFP_CharacterMovementComponent::DoJump(){
if (CharacterOwner) {
if (!bConstrainToPlane || FMath::Abs(PlaneConstraintNormal.Z) != 1.f){
Velocity.Z = JumpZVelocity + JumpZVelocity * Cast<ATFP_Character>(CharacterOwner)->JumpPower;
SetMovementMode(MOVE_Falling);
return true;
}
}
return false;
}