- Home /
How to make RigidBody's movement using fixed movement/velocity and other forces all together?
Hello,
My character is moving using a RigidBody with mouse clicks (like an RPG). What i am trying to do is my character to use as his movement a fixed movement (created when i click to the destination) plus the other forces (knockbacks etc) that may are applied on the character.
Practically, i want to 'say' to the RigidBody component that when it calculates the velocity based on the forces applied on the character, add to the calculated result of the velocity the amount of character's speed.
Is there any way to do that? I know how to change the velocity, but changing it results on losing the impact from other forces. I also cant find a way to express the fixed movement/velocity as a force.
(I am using drag and character mass which is mostly needed for the extra forces)
Thank you in advance
Answer by SebasSBM · Dec 15, 2016 at 01:08 PM
The way I interpret "fixed movement and velocity" (not sure if I am understanding or misunderstanding, but here goes), brings me to this approach:
An alternative to @Glurth's approach would be:
Determine direction for the "fixed velocity" as a Vector3 (substracting character's position from mouse's position, if you will so. Here's an example on how to obtain position in 3D space through mouse ).
Change magnitude to desired amount of force.
Make this vector the velocity of your character's Rigidbody.
Apply the rest of the forces.
A little code example:
float my_speed = 3.0f;
//You can embed the example linked above into a custom function
// let's call it MyMousePosition for this example
Vector3 mouse_pos = MyMousePosition(Input.mousePosition);
Vector3 directed_vel = mouse_pos - transform.position;
directed_vel = directed_vel.normalized * my_speed;
rigidBody.velocity = directed_vel;
//Apply other forces from here on
I noticed right now: magnitude
is ReadOnly. But I got the feeling that it can be done in a similar way... I'll update the answer after dealing with this mistake of $$anonymous$$e :P
EDIT: O$$anonymous$$, magnitude issue fixed (I ended up doing the same as @Glurth's snippet without noticing xD). I hope this helps.
As a complementary piece of information, I reccommend to take a look to Understanding Vector Arithmetic from Unity Documentation.
Thanks for your response.
I am familiar with Vector arithmetic and i have already made the mouse to screen movement. I am using gravity so if i add a force for movement, it is fading away frame to frame (friction) -which is useful to me on the external forces-. To have a constant movement representing the speed of the character, i need to change the velocity every frame. So in the end, i want my player to have a velocity of the movement's constant velocity + the impact of external forces which correspond to every frame on the RigidBody's velocity.
I'm trying to get closer to understand what you and @Glurth are saying. I have replied to @Glurth with an example code that may help us end to a conclusion.
If the only problem with this approach is gravity: gravity itself is just a force. You can disable the Use gravity
option in your Rigidbody
, and use this ins$$anonymous$$d (after the answer's example):
Vector3 my_gravity = new Vector3(0f, -9.81f, 0f);
rigidbody.AddForce(my_gravity, Forcemode.Force);
This way you make it compute gravity after applying constant velocity.
However, I am not sure if this would override friction or not...
It's the same thing (i checked it out). Generally, it would be okay if the external forces were a type of: "Apply it in every frame". In my case though, when my character receives a strong force in a frame, its effect will last till the next [X] frames, until the friction eli$$anonymous$$ates it.
So if i set the velocity in the next frame to a new value, it will remove the effect of the previous force that was applied. And that's actually my biggest problem.
Answer by Glurth · Dec 14, 2016 at 04:39 PM
You can apply a force in multiple ways with unity. https://docs.unity3d.com/ScriptReference/ForceMode.html
I suspect you want to use the VelocityChange mode, for your normal/ move-towards-destination movement. (and keep the default / ForceMode.Force for other stuff)
One thing to keep in mind: this force will change every cycle! As you character get moved off course, for example, the direction of the destination, relative to your character, will change ad need to be recomputed.
I've tried Force$$anonymous$$ode.VelocityChange but the problem is that i want to 'express' the movement of the player as velocity because i want it to be fixed (so it will have the same magnitude). And i want to have that velocity + the velocity made/calculated from other forces.
So it would be like:
void Update(){
myRigidBody.velocity = Vector3.forward * speed + velocity_created_from_other_forces;
}
Of course i can't do that as there is no method that gives me this functionality (as far as i know). Expressing a fixed/constant movement on a RigidBody through a force seems crazy/hard/impractical to me. I keep insisting on using a RigidBody because it really helps me on simulating the other external forces that affect the character.
Using a RigidBody for the external forces and Transform.Translate for the movement doesn't seem a good idea either.
Any thoughts?
I'm not positive I understand what your looking for because your expression will result in a velocity who's magnitude varies with the "other Forces".
If you are trying to have the character only move at a particular "speed" (velocity magnitude), you can normalize the velocity, and multiply by speed.
rb.velocity= rb.velocity.normalized * speed;
This will keep it's direction the same, but set the magnitude to 'speed'
Is that what you meant?
Thanks for responding,
Not exactly. (I didn't use '.normalized' because Vector3.forward basically is a normalized vector.)
The thing is i want my character's movement to be based on the character speed (walking) plus the rest of the forces that affects him.
For example assume that my player is moving with (Vector3.forward speed) velocity and he gets hit by a force from an object in one random direction (lets say his right side). So then in the next frame, i want my player to have a velocity of Vector3.forward speed + [the external force's impact on the player (myRigidBody.AddForce(Vector3.left)) that corresponds in that frame as velocity]
I am not quite sure if what i am saying is contradicted based on how the velocity gets calculated from the forces. If the option of manually setting the velocity of a RigidBody didn't exist, i wouldn't bother.
It won't work. Take for example:
$$anonymous$$y character has a velocity speed of 5 and/so his walkThreshold is 5. His direction is Vector3.forward. If an external force hits my character with lets say a -2 result in its velocity magnitude (in the opposite direction), then in the next frame i'll see my velocity equals to 3. So, then myVelocity < walkThreshold and i'll be adding walking force till i reach the walkThreshold again. (The force doesn't even need to be the exact opposite to have this as a result.) In this case, my velocity with magnitude 3 will be the result of 5 (walking) - 2 (force in opposite direction) = 3. But as i said, in the next frame i'll see that '3' as my walking speed and not as the sum of my walking speed with the external force.
The problem can't be solved with this approach.
Answer by mrpmorris · Dec 16, 2016 at 06:00 PM
Sounds like you want the "shortest path" algorithm. I haven't used this, but A* Pathfinder comes highly recommended
https://forum.unity3d.com/threads/a-pathfinder-based-on-waypoints.80366/
Interesting info, but it is not really the root of my problem. $$anonymous$$y player just walks straight forward to a specific point anyway.
The thing i can't 'solve' is how to express the movement of a RigidBody, as a fixed constant movement (e.g. walking) plus the impact of the external forces that my character may get (all together).
Your answer
Follow this Question
Related Questions
Rigidbody physics; forces doesnt move gameobject in a desired way 1 Answer
Moving rigidbody (Player) with addForce or Velocity ? 1 Answer
Do rigid bodies with force add force to other objects? 0 Answers
Rocket jumping - rigid / transform problem 0 Answers
VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer