x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

C++ making a jump function.

Hello.

I have a problem regarding making a jump function for my spherecomponent.

I have added a jump function that looks like the following

void ACollidingPawn::JumpForce() { OurMovementComponent->AddInputVector(GetActorUpVector(), true); }

I have bound this action to a key on my keyboard in the input settings aswell as bound it in the code.

However when I press this key after compiling and starting the game, it only moves so little up in the air. it's barely 5 pixels.

I have searched allover the internet for a custom jump function that you can use but all I get is blueprint answers. I read about an addImpulse() function that I can use, however trying to apply this method to my movementComponent only gives me the AddRadialImpulse() function, which isn't what I want.

Are there any answers out there, i'd be very glad to accept them.

thanks // Karl

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "Howto_Collision.h"
 #include "CollidingPawn.h"
 
 
 // Sets default values
 ACollidingPawn::ACollidingPawn()
 {
     // 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;
 
     USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
     RootComponent = SphereComponent;
     SphereComponent->InitSphereRadius(40.0f);
     SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
     SphereComponent->SetSimulatePhysics(true);
     StaticMesh = SphereComponent;
     UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
     SphereVisual->AttachTo(RootComponent);
 
     static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
     if (SphereVisualAsset.Succeeded())
     {
         SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
         SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
         SphereVisual->SetWorldScale3D(FVector(0.8f));
     }
 
     OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
     OurParticleSystem->AttachTo(SphereVisual);
     OurParticleSystem->bAutoActivate = false;
     OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
     static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
 
     if (ParticleAsset.Succeeded())
         OurParticleSystem->SetTemplate(ParticleAsset.Object);
 
 
 
     USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));
     SpringArm->AttachTo(RootComponent);
     SpringArm->RelativeRotation = FRotator(-45.0f, 0.0f, 0.0f);
     SpringArm->TargetArmLength = 400.0f;
     SpringArm->bEnableCameraLag = true;
     SpringArm->CameraLagSpeed = 3.0f;
     SpringArm->bInheritPitch = false;
     SpringArm->bInheritYaw = false;
     SpringArm->bInheritRoll = false;
     UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
     Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);
 
     OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustomMovementComponent"));
     OurMovementComponent->UpdatedComponent = RootComponent;
 
     AutoPossessPlayer = EAutoReceiveInput::Player0;
 
     //SphereComponent->AddImpulse(FVector(0, 0, 1000));
 }
 
 // Called when the game starts or when spawned
 void ACollidingPawn::BeginPlay()
 {
     Super::BeginPlay();
 
 }
 
 // Called every frame
 void ACollidingPawn::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
     //JumpForce();
 }
 
 // Called to bind functionality to input
 void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
 {
     Super::SetupPlayerInputComponent(InputComponent);
 
     InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);
     InputComponent->BindAction("JumpForce", IE_Pressed, this, &ACollidingPawn::JumpForce);
 
     InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
     InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
     InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);
     InputComponent->BindAxis("Jump", this, &ACollidingPawn::Jump);
 
 }
 
 UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
 {
     return OurMovementComponent;
 }
 
 void ACollidingPawn::MoveForward(float AxisValue)
 {
     if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
     {
         OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
     }
 }
 
 void ACollidingPawn::MoveRight(float AxisValue)
 {
     if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
     {
         OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
     }
 }
 
 void ACollidingPawn::Turn(float AxisValue)
 {
     FRotator NewRotation = GetActorRotation();
     NewRotation.Yaw += AxisValue;
     SetActorRotation(NewRotation);
 }
 
 void ACollidingPawn::ParticleToggle()
 {
     if (OurParticleSystem && OurParticleSystem->Template)
     {
         OurParticleSystem->ToggleActive();
     }
 }
 
 
 void ACollidingPawn::JumpForce()
 {
     if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
     {
         OurMovementComponent->AddInputVector(GetActorUpVector(), true);
     }
 
 }


Product Version: UE 4.9
Tags:
more ▼

asked Oct 12 '15 at 03:43 PM in C++ Programming

avatar image

KarlNihlen
1 1 2 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hello,

The GetActorUpVector() returns a vector of length one (a unit vector I believe it's called in english). So you are basically pushing it up 1 unreal unit, which is equivalent to 1cm. You should scale that value by a number, say 300.f, and check what happens.

 OurMovementComponent->AddInputVector(GetActorUpVector() * 300.f, true);

If it now gets up then that was the problem.

more ▼

answered Oct 13 '15 at 11:31 PM

avatar image

Suui
141 4 6 20

avatar image KarlNihlen Oct 23 '15 at 06:37 AM

Hello suui!

I have tried this and its not working but what I have figured out is that I can apply a static mesh of sorts to the sphere component itself and add an impulse to the mesh.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question