While Pressing Left ShitfButton Character start to move fast but Capsule Component(Collider) Left Behind in c++ Any Way to Fix
This really depends on how you are moving the character. Is this using a root motion animation or in-place? And also, how are you mapping the input into the character movement? For example, are you using AddMovementInput
?
if ((Controller != nullptr) && (Value != 0))
{
FRotator NewRotation = Controller->GetControlRotation();
FRotator YawRotation(0.0f, NewRotation.Yaw, 0.0f);
FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}
Could it be your character’s movement mode? Based on what I am seeing here it seems like if MovementMode
is MOVE_Walking
, vertical velocity will be zero. So, I imagine it wouldn’t matter if you apply vertical movement input, it would still be zero.
I will send code have look:-
// Fill out your copyright notice in the Description page of Project Settings.
#include “MainCharacter.h”
#include “GameFramework/SpringArmComponent.h”
#include “Camera/CameraComponent.h”
#include “Kismet/GameplayStatics.h”
#include “Engine/World.h”
#include “Components/CapsuleComponent.h”
#include “GameFramework/PlayerController.h”
#include “GameFramework/CharacterMovementComponent.h”
// Sets default values
AMainCharacter::AMainCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("Camera Boom"));
CameraBoom->SetupAttachment(GetRootComponent());
CameraBoom->bUsePawnControlRotation=true;
CameraBoom->TargetArmLength = 600.f;
GetCapsuleComponent()->SetCapsuleSize(48.f, 105.f);
FollowCameraBoom = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera Component"));
FollowCameraBoom->SetupAttachment(CameraBoom,USpringArmComponent::SocketName);
FollowCameraBoom->bUsePawnControlRotation=false;
bUseControllerRotationRoll = false;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->JumpZVelocity = 500.f;
GetCharacterMovement()->AirControl = 0.2f;
GetCharacterMovement()->RotationRate = FRotator(0.0f, 450.0f, 0.0f);
Health = 50.f;
MaxHealth=100.f;
Stamina=120.f;
MaxStamina=150.f;
Coins = 0.f;
RunningSpeed = 650.f;
SprintingSpeed = 950.f;
bShiftKeyDown = false;
//Initiation Enums
MovementStatus = EMovementStatus::EMS_Normal;
StaminaStatus = EStaminaStatus::ESS_Normal;
StaminaDrainRate = 25.f;
MinSprintingStamina = 50.f;
}
// Called when the game starts or when spawned
void AMainCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMainCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
float DeltaStamina = StaminaDrainRate * DeltaTime;//how much stamina should change in this particular Tick Delta.
switch (StaminaStatus) {
case EStaminaStatus::ESS_Normal://we we are in the Normal Stage What should we Do.
if (bShiftKeyDown)
{ //Here is a Small Changes What would put us into the
//Below Minimum State if we are Going to cross the minimum.
if (Stamina - DeltaStamina <= MinSprintingStamina)//if this the State then we are crossing the normal state over into the below minimum State.
{
SetStaminaStatus(EStaminaStatus::ESS_BelowMinimum);
Stamina -= DeltaStamina;
}
else
{
Stamina -= DeltaStamina;//We are Decreasing Stamina From the Normal Stage Not From the BelowMinimun Stage.
}
// update the Movement
SetMovementStatus(EMovementStatus::EMS_Sprinting);
}
// Shift key up And This Means We Should Not be Sprinting. To Replenishing.
else//When Shift key is up
{
if (Stamina + DeltaStamina >= MaxStamina)
{
Stamina = MaxStamina;//So that stamina does not pass MaxStamina. While increasing
}
else
{
Stamina += DeltaStamina;
}
// update the Movement
SetMovementStatus(EMovementStatus::EMS_Normal);//ShiftKeyis Up Show It gonaa Be in Normal.
}
break;
case EStaminaStatus::ESS_BelowMinimum:
if (bShiftKeyDown)
{
if (Stamina - DeltaStamina <= 0.f)
{
SetStaminaStatus(EStaminaStatus::ESS_Exhausted);
Stamina = 0;
SetMovementStatus(EMovementStatus::EMS_Normal);// If we are in Exhausted Stage then we should be in Normal Stage.
}
else// When ShifhtKeyUp Is Down
{
Stamina -= DeltaStamina; //if we are still in the below minimum state And We are not Exhausted yet and Shift key is Down
SetMovementStatus(EMovementStatus::EMS_Sprinting);
}
}
else//When Shift key is up
{
if (Stamina + DeltaStamina >= MinSprintingStamina)
{
SetMovementStatus(EMovementStatus::EMS_Normal);
Stamina += DeltaStamina;
}
else
{
Stamina += DeltaStamina;
}
SetMovementStatus(EMovementStatus::EMS_Normal);
}
break;
case EStaminaStatus::ESS_Exhausted:
if (bShiftKeyDown)
{
Stamina = 0.f;
}
else//Shiftkeyup
{
SetStaminaStatus(EStaminaStatus::ESS_ExhaustedRecovering);
Stamina += DeltaStamina;
}
SetMovementStatus(EMovementStatus::EMS_Normal);
break;
case EStaminaStatus::ESS_ExhaustedRecovering://while we are sprinting we should not able to Sprinting.
if (Stamina + DeltaStamina >= MinSprintingStamina)
{
SetStaminaStatus(EStaminaStatus::ESS_Normal);
Stamina += DeltaStamina;
}
else
{
Stamina += DeltaStamina;
}
SetMovementStatus(EMovementStatus::EMS_Normal);
break;
default:
;
}
}
// Called to bind functionality to input
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAction(“Jump”,IE_Pressed,this,&ACharacter::Jump);
PlayerInputComponent->BindAction(“Jump”, IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Sprinting", IE_Pressed, this, &AMainCharacter::ShiftkeyDown);
PlayerInputComponent->BindAction("Sprinting", IE_Released, this, &AMainCharacter::ShifhtKeyUp);
PlayerInputComponent->BindAxis("MoveForward", this, &AMainCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMainCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("Lookup", this, &APawn::AddControllerPitchInput);
}
void AMainCharacter::MoveForward(float Value)
{
if ((Controller != nullptr )&& (Value !=0))
{
FRotator NewRotation = Controller->GetControlRotation();
FRotator YawRotation(0.0f, NewRotation.Yaw, 0.0f);
FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
void AMainCharacter::MoveRight(float Value)
{
if ((Controller != nullptr) && (Value != 0))
{
FRotator NewRotation = Controller->GetControlRotation();
FRotator YawRotation(0.0f, NewRotation.Yaw, 0.0f);
FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}
}
void AMainCharacter::DecerementHealth(float Amount)
{
if (Health-Amount<=0.f)
{
Health -= Amount;
MainDie();
}
else
{
Health -= Amount;
}
}
void AMainCharacter::MainDie()
{
}
void AMainCharacter::IncreamentCoins(int32 Amount)
{
Coins += Amount;
}
void AMainCharacter::SetMovementStatus(EMovementStatus status)
{
MovementStatus = status;
if (MovementStatus == EMovementStatus::EMS_Sprinting)
{
GetCharacterMovement()->MaxWalkSpeed = SprintingSpeed; //Setting To Sprinting Speed.
}
else
{
GetCharacterMovement()->MaxWalkSpeed = RunningSpeed;
}
}
void AMainCharacter::ShiftkeyDown()
{
bShiftKeyDown = true;
}
void AMainCharacter::ShifhtKeyUp()
{
bShiftKeyDown = false;
}