Download

Please tell me What is wrong with this code

I’m fairly new to Unreal, I’m trying to learn things around please correct if the following code was wrong. I wanted to implement a jump mechanics in the car template, after looking around I understood that implementing a jump would be adding impulse at location so tried this code and unreal crashes every time.
Tes3232 - Microsoft Visual Studio.jpg Help me !

This was in advanced car template unfortunately both crashes
ff027d8b59a276d8f9d5c929c0706a89df87ba76.jpeg

Fixed: using UPrimitiveComponent::AddImpulse Tahanks @ Shockwave-peter
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/AddImpulse/index.html



void ABOTRODS_V1Pawn::OnJump()
{
	const float impulseAmount = 500.0f;
	APlayerController* currentPlayer = UGameplayStatics::GetPlayerController(GetWorld(), 0);
	GetMesh()->AddImpulse( currentPlayer->GetPawn()->GetActorUpVector()*impulseAmount,  FName(TEXT("Vehicle")),true);
}


using UPrimitiveComponent::AddImpulseAtLocation
to use add impulse at location we do need to calculate the mass of the Actor along with impulse


UpdatedPrimitive->AddImpulseAtLocation(GetActorUpVector() * UpdatedPrimitive->GetMass() * impulseAmount, GetActorLocation());

Where is it crashing? There are quite a few things you can check to see where its going wrong

  • Check that currentComp is != nullptr, GetOwner.
    Does the owner have the same Locations as the mesh?

if your crash is access violation in the line currentComp->AddForceAtLocation() your variable is nullptr

yes Owner has the same location as the mesh because it is in hierarchy I tried nullptr check still it crashes
620afa3124bd6cafd304c12da95a7e9291cdbf6d.jpeg

Tes3232 - Microsoft Visual Studio.jpg

Thank you :slight_smile: i will check for nullptr

Edit: still i get the same trouble

Still the same :frowning:

Tes3232 - Microsoft Visual Studio Code 382.jpg

CRASH REPORT

Where is it crashing?

Step through the code until you isolate the statement that is causing the problem.

It is Crashing at the last line when i add the impulse to the primitive component;

Something like this should work:


	
UpdatedPrimitive->AddImpulse(GetActorUpVector() * impulseAmount, NAME_None, true);


Third parameter is very important in your case :slight_smile:

sure I will try with the bone name :slight_smile:

Please read my previous answer again. I was talking about THIRD parameter, not a second one. Second one in your case should remain default NAME_None, what you actually need to do is specify third parameter as true (by default it’s false) to actually affect velocity.

Yes Shockwave the code is working, the third value makes a lot difference it makes the value of the current actor changeable.

9d23f1983d8cc8d9380b7e08b11bdb43e3b37d4a.jpeg



void ABOTRODS_V1Pawn::OnJump()
{
	const float impulseAmount = 500.0f;
	APlayerController* currentPlayer = UGameplayStatics::GetPlayerController(GetWorld(), 0);
	GetMesh()->AddImpulse( currentPlayer->GetPawn()->GetActorUpVector()*impulseAmount,  FName(TEXT("Vehicle")),true);
}


Add impulse at location wasn’t working though. i think i have to cast the ray towards the ground and get location of it and add impulse from there, probably.

I just discovered this a few days ago.
If you start the editor through Visual studio in the debug mode it will tell you why the editor crashed.

Please post the code inside the


 tags that way people can debug easier.

Oh,that sounds interesting i will try that.

Sorry for the print screen, Yea i will do that from next time.

It didn’t work because you forgot to add mass to your impulse calculation. Something like this should work:


UpdatedPrimitive->AddImpulseAtLocation(GetActorUpVector() * UpdatedPrimitive->GetMass() * impulseAmount, GetActorLocation());

Thanks again, yes it did the trick calculating along with mass did the magic, my bad i did not understand the concept. Impulse should be produced at the amount that could lift the weight of the object, because it is an external force.
as we do normal addImpulse as the force was produced from its own body, so we do not need to calculate the mass ?

No, we were directly affecting velocity because of the third parameter set to true.

oh ok, but i Have some weird problem that only my root bone is getting affected by the Impulse and not my other bones in the skeltal mesh. Do you ahve any idea why that happens ?

for example


GetMesh()->AddImpulse(GetActorUpVector()*impulseAmount, FName(TEXT("Vehicle")) ,true);

works fine and that is root mesh and no vertices weighted


GetMesh()->AddImpulse(GetActorUpVector()*impulseAmount, FName(TEXT("Hub_BL")), true);

does not work it is not a root bone and with vertices weighted