Download

Add Force

I have a weird problem that needs an equally weird solution. Basically, I am trying to have my player land on a platform while retaining ‘overlap all’ for the player and the platform (Paper2D, but shouldn’t matter).


CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());

Shouldn’t this counteract the Z gravity and hold the player in place while it runs on the tick?

I asked a similar question on the answer hub: Simple Collision Question - UE4 AnswerHub


void ATestPlatCharacter::Tick(float DeltaSeconds){
	Super::Tick(DeltaSeconds);
	GEngine->ClearOnScreenDebugMessages();

	FVector coords = GetActorLocation();
	x = coords.X;
	z = coords.Z;
	depth = coords.Y;

	if (place_meeting<APlatformParent>(x,z - 1)) {
		free = 0;
		
	}
	else {
		free = 1;
	}

	if (free == 0) {
		/* Does not work. */
		//CapsuleComponent->SetEnableGravity(false);
		//CapsuleComponent->AddImpulseAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
		//CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
		//CapsuleComponent->ComponentVelocity = FVector(0, 0, 0);
		//CapsuleComponent->SetEnableGravity(false);
		//bSimGravityDisabled = true;

		/* Kinda works but doesnt'. */
		//CharacterMovement->Velocity = FVector(0, 0, 0);
		//CharacterMovement->Velocity.Z = 0;

		/* This tries to work, but then there is no friction. Perhaps compensate? */
		//CharacterMovement->GravityScale = 0.0f;
		//CharacterMovement->Velocity.Z = 0;

		CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
		
	}
	else {
		//The character is not on the ground.
	}

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::SanitizeFloat(GetVelocity().Z));
	//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString("free = ") + FString::FromInt(free));
}

shouldnt it be GlobalGravityZ * character mass or something like that


CapsuleComponent->AddForceAtLocation(FVector(0, 0, (GetWorld()->GetGravityZ() * CapsuleComponent->GetMass()) * -1), GetActorLocation());

This didn’t do it, but I appreciate the reply anyhow.

Force = mass * acceleration. Acceleration = change in velocity / time. In your case, acceleration = -1*velocity.z / deltatime. Force = acceleration * mass. SO… You need to multiply what you have with the character’s mass and deltatime. Using gravity wouldn’t work because it would only stop gravity, but you would still keep falling at the same speed as when gravity was turned off.

On top of that, I can’t turn off gravity anyhow (it messes with friction). This should have been obvious, but my brain has been fried converting a project from Game Maker. I tried what you said, and the character is still plowing right through the platform.


CapsuleComponent->AddForceAtLocation(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()), GetActorLocation());

Am I doing this right? Basically, I am trying to apply an equal force against gravity to the character while it is contacting the platform to simulate a solid contact. I have all the collision code and such worked out, this is the only piece holding us back.

I tried AddForce as well:


CapsuleComponent->AddForce(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()));

Thanks for replying, it is very appreciated.

It doesn’t even slow down? Maybe you should add force to the movement component instead? IDK.

Alright, some progress, the following line of code will cause the character to bounce for some reason, and then slowly make its way into the ground. Also, friction is not being applied again.


CharacterMovement->AddForce(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()));

Shouldn’t it be *** DeltaTime** instead of /?

I think it would be velocity divided by time for that part. This solution will not work anyhow, as it makes you float (as it should) and nulls out friction. I was unaware of how advanced the collision channels were in UE4. So I am instead going to try a raycast and setting the character collision channel against world statics to see if I can get the same effect minus the headache.

I got it working, and I will be posting a tutorial here soon. Thank you all for your help.

Using custom collision channels solved our problem:

Answer here:

Thanks to BadderThanBad for pointing me in the right direction.