Is there a way to push an actor to a certain direction with a certain force?
Why can’t you check documentations instead of going to forums and ask every little thing you stumble upon? Sounds like help vampire… Sorry for being rude; but you’re not learning how stuff works like this.
There are AddImpulse/AddForce/AddTorque functions of UPrimitiveComponent avilable.
You are right, it is a slow way for me to learn as well.
Is there a better way to get to such things or are you simply going over the whole API? (I didn’t see any other reference to UPrimitiveComponent anywhere else)
Did you check the whole hierarchy of Actors to get the answer?
In Unity,CryEngine or other engines it’s always AddForce or similar.So no we didnt check whole hierarchy,its most likely something close to AddForce
yeah well I don’t have that kinda previous experience I’m trying to learn how to learn to use this engine without having to open a thread for every little bump in the way, assuming I don’t know anything.
As for the AddForce:
I tried to add the current direction as the force vector:
FVector CurrentDirection = FRotationMatrix(GetActorRotation()).GetScaledAxis(EAxis::X);
GetRootPrimitiveComponent()->AddForce(CurrentDirection);
Doesn’t seem to work. Since the docs mentioned the magnitude of the vector determines the force applied, I tried to multiply it, as well as use FVector.ClampMaxSize() as the clamping functions are the only ones containing the word “magnitude” in their description. (Feels stupid, but I’m trying to work with the knowledge I have which is close to nothing)
I tried calling it before and after the Tick.
What am I doing wrong?
Probably the fastest way would be to have API page opened all the time and searching by main keyword, like force in this case. But no, i didn’t check anything to see if it’s there, these features are obviously in the engine, the question is what component has them, and it’s either mesh or physics body.
I have Visual Assist X installed, it’s much better than IntelliSence and much faster as well. I suggest you get this.
It also allows you to do something like this: UPrimitiveComponent:: and then you should get list of all the functions this class implements, though it’s still buggy and slow sometimes with huge projects like UE.
Update for your post: you should probably add it in real-time, maybe even in tick w/o any condition to see if it works properply, not sure if scaled axis will work as intended, just get normalized axis vector and multiply by amount of force you want to apply.
GetRootPrimitiveComponent()->AddForce(GetActorForwardVector() * 200.0f);
as example.
Searching the Documentation doesn’t really give good results (I tried searching Force), seems like I’ll have to browse through the classes everytime, correct me if I’m wrong.
Visual Assist X - I’ll give it a try, good to know there’s a faster option than IntelliSence.
Tried your example in tick (before and after Super just in case), didn’t do anything.
AddForce etc only work if the actor is set to simulate physics. What sort of actor are you trying to push?
For something that is not simulating physics, like a Character for instance, you will want to apply a velocity directly and allow the non-physics simulation (in this case the MovementComponent) to respond. For example look at ACharacter::LaunchCharacter() or UCharacterMovementComponent::AddMomentum().
I understand.
I tried LaunchCharacter - there are 2 problems with it:
-Every time I’m using it the character jumps a little
-The higher the terrain I’m standing on at the moment of activation, the stronger the force applied. What’s the logic behind this?
Here’s the line I’m trying:
LaunchCharacter(GetActorForwardVector() * 10000.0f, false, false);
As for the booleans in the call, I’m not entirely sure what they’re doing, but I tried all 4 possible combinations and none seemed to affect anything.
AddMomentum - didn’t seem to do anything other than a tiny jump in the animations.
I didn’t really understand what each vector represents and there’s no description in the docs, so I’m most likely doing it wrong:
CharacterMovement->AddMomentum(CurrentDirection * 5000.0f, CurrentDirection * 10000.0f, false);
An attempt to understand what the vectors represent which didn’t work either:
CharacterMovement->AddMomentum(CurrentDirection * 10000.0f, GetActorLocation(), false);
As others stated Character class is not set for simulating physics.
So if I understand you correctly,you want your character to be applied force whose magnitude is determined by where you stand on the terrain ?
I dont know about LaunchCharacter method but you can do the following,
Check if character is on the terrain,
check character’s height from the terrain (Character.Z - terrain.Z)
Divide by some scalar and
AddImpulse(DirectionOfYourForceVector*MagnitudeOfYourForceScalar,“”,true)
AddImpulse and AddForce methods are different.Try them in action.AddImpulse is used for momentary bursts like in explosions etc.AddForce is more likely used for things you want it over time.You have to give really big numbers in order to AddForce work because it involves gravity force too.
I’m not expert on this but AddForce(YourForceDeltaTime) works like
FinalForce = AllForcesActingOnBody + YourForceDeltaTime.
On the contrary, that is one of the problems I have with the function, I don’t want it to happen.
Read what Zak said about AddImpulse.
You started by saying the Character class is not set for simulating Physics, yet continued with advising me to use AddImpulse which is a contradiction. Let me know if I misunderstood you or if I’m missing something.
Contradiction? Yes.But I was teaching how AddImpulse and AddForce work.It says “rigid bodies” in the code so why would I tell you to use AddImpulse for character ? … You cant even reach that method.
If you want your character to launch up use this
CharacterMovement->Velocity += Direction*Magnitude
Velocity seems to be the right way to go, however I still have the problem that the force changes depending on how high the character is (even if he’s jumping):
CharacterMovement->Velocity += GetActorForwardVector() * 10000.0f;
LaunchCharacter basically just sets a pending launch velocity on the CharacterMovementComponent, and once it ticks it will apply the velocity and set the movement to falling. Those bools indicate whether the velocity is set directly or added to the current velocity.
AddMomentum takes a momentum which is (Velocity * Mass) of the object applying the force. CharacterMovementComponent->Mass will affect the result.
Directly setting velocity is also a fine way to do it. In your case if you are moving directly forward and already walking, you may quickly run lose your velocity due to friction and deceleration. Depending on your intent, you may want to angle slightly up and also set movement mode to Falling.
Right so friction is the problem?
I set the movement mode to falling but I suppose it is set right back since there’s friction with the floor, if I set the angle slightly up it’d mean ending up in the air no? I want the dash to be completely on the ground.
How’d you approach making a dash?
Still need an answer
You’re fine keeping it in walking mode if you don’t want to leave the ground. You can adjust the friction and/or deceleration setting based on your needs during the dash, and then restore it to the previous value afterwards.
For example you may want it to be based on a timer (you dash for 0.2 seconds), distance (you can go up to 10 meters), etc. After the dash is complete, turn friction/deceleration back up.
When I prototyped a dash I did something similar: I launched the character at a high speed, and then lowered friction if I was over the max ground speed, restoring it once I wasn’t exceeding the speed. Basically in the Tick I checked if IsExceedingMaxSpeed() returned true and used one set of friction/decel values, and used the original values if not.
I ended up setting ground friction to 0 following your suggestion, and forcing the same very high acceleration in Tick while dashing, then setting acceleration to 0 when dash is over. (resulting in constant high speed over a fixed period of time)
However I’d still like to clarify stuff with you since I’m experimenting in order to learn:
When you mentioned to friction, did you refer to CharacterMovement->GroundFriction?
What did you refer to when you mentioned deceleration? How saw no way to access such a variable in CharacterMovement, and the Acceleration variable seems to be protected so I have no access to modifying it.
Is there a built in way in Unreal Engine to create a timer and count distance or do you do it manually? (not that it’s a problem, but curious)
When you launch the character at a high speed did you use LaunchCharacter? Didn’t you end up with 2 different speeds in ground an air (less with friction disabled, maybe it was solved by disabling deceleration which I couldn’t figure out how to do)
Could you please help me understand the dash you prototyped? Did it result in very high speed over a short period of time? Why disable friction and deceleration only when exceeding max speed and not simply disable them regardless while dashing?
Thank you for you answers so far you’ve been very helpful
Btw I read every word in your answers even if in my replies I just bring up more issues, extra stuff you say are not in vain.
Still need an answer
Hi, sorry, I was out most of today.
Yes, for friction I mean CharacterMovement->GroundFriction.
There is a setting called CharacterMovement->BrakingDecelerationWalking (or similar, I don’t have the code with me right now). This deceleration is applied when there is no active input (no acceleration), or when exceeding max speed. Friction is always applied.
There are timer functions in the engine (don’t recall off-hand), but for something like checking distance traveled, you would have to do that yourself.
In my dash example, I went with low friction only when over a certain speed mostly to give the feeling of skidding out of control until the speed went down, after which you had better traction and regained control. The “dodge” or “dash” was simply only an initial high velocity in a certain direction, and then you regained most of your control once the speed was low enough (you could still steer while going fast, but poorly).