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)
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 ).
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 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.