Codezeero
(Codezeero)
June 27, 2016, 8:26pm
1
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.
Help me !
This was in advanced car template unfortunately both crashes
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?
ZkarmaKun
(ZkarmaKun)
June 27, 2016, 10:30pm
3
if your crash is access violation in the line currentComp->AddForceAtLocation() your variable is nullptr
Codezeero
(Codezeero)
June 27, 2016, 10:42pm
4
yes Owner has the same location as the mesh because it is in hierarchy I tried nullptr check still it crashes
Codezeero
(Codezeero)
June 27, 2016, 10:43pm
5
Thank you i will check for nullptr
Edit: still i get the same trouble
mid_gen
(mid_gen)
June 28, 2016, 8:46am
7
Where is it crashing?
Step through the code until you isolate the statement that is causing the problem.
Codezeero
(Codezeero)
June 28, 2016, 10:00am
8
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
Codezeero
(Codezeero)
June 29, 2016, 6:51am
10
Something like this should work:
UpdatedPrimitive->AddImpulse(GetActorUpVector() * impulseAmount, NAME_None, true);
Third parameter is very important in your case
sure I will try with the bone name
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.
Codezeero
(Codezeero)
June 29, 2016, 8:24am
12
Something like this should work:
UpdatedPrimitive->AddImpulse(GetActorUpVector() * impulseAmount, NAME_None, true);
Third parameter is very important in your case
Yes Shockwave the code is working, the third value makes a lot difference it makes the value of the current actor changeable.
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.
Blue_man
(Blue_man)
June 29, 2016, 9:36am
13
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.
MrGoatsy
(MrGoatsy)
June 29, 2016, 9:58am
14
Please post the code inside the
tags that way people can debug easier.
Codezeero
(Codezeero)
June 29, 2016, 10:56am
15
Oh,that sounds interesting i will try that.
Codezeero
(Codezeero)
June 29, 2016, 10:59am
16
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());
Codezeero
(Codezeero)
June 30, 2016, 9:15am
18
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.
Codezeero
(Codezeero)
July 1, 2016, 8:36am
20
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