How to add a direction without adding a force?

Hello everyone.

I have a question that I cannot solve, my problem is that I add a force with addforce and a vector3 to an object, but in this way I add the force and the direction of the force all with the vector3 and I want to put the direction on one side and force for another.

Do you know how I could do it?

I have tried doing a normalize () but it doesn’t work for me, it also keeps adding force and apart from normalize I don’t know what else is there to do this.

I have it like this:

DirectionDer = (PosHandDer - PosBall);

DirectionDer.Normalize ();
rbBall.AddForce (DirectionDer * (CurrentForce * MultiplyForce));

Before I had the normalize in the first line but to test I removed it from the first line and made a new one just to apply the normalize, but I didn’t change anything)
The force to be applied comes to me from two different places

Thank you very much in advance .
I think I have explained myself well: P

sorry for my bad English

I don’t think I understand your question. AddForce adds a force, I don’t see what you expect it to do when only adding a direction?

You could:

  • Store the direction somewhere and then use that direction when you add the force. Then you can change the direction independently of calling AddForce.
  • Use Transform.forward (or any of the other transform-relative direction vectors) to add a force and then rotate the object to only change the direction?
2 Likes

In my game I have a variable that says in the direction the object should go and another that indicates with what force it has to be pushed, but the variable of the direction also adds force, I want to know how to do it so that the variable Direction only indicate where the object is going to go without adding any additional force or adding an always equal and non-variable force, so that I can apply the force in which said object is going to go out that I want without adding any force additional.

Keep in mind that the direction variable is random, so it applies a lot of force to me and sometimes it applies very little, so it is impossible to control the force in which the object comes out.

For what you indicate I can add an address without modifying the current force of the object, how can that be done? I always apply a direction it adds a force

I don’t know if I have explained myself well but I don’t know how to explain myself better: P

In this case, normalizing the direction vector and then multiplying it with the force is the right approach.

The length of the final vector you pass to AddForce needs to match the amount of force you want to apply. Normalizing the direction will make it have a length of one, then multiplying it with the actual force turns it into the length it needs.

Note that if you just do AddForce(direction) with the normalized direction, it will still apply a force of one. This is expected, the force multiplication takes care of controlling the actual force (e.g. multiplying with 0 will result in a zero-vector and no force being applied).

2 Likes

It is what I thought and it is how I have applied it but it continues to apply a force to me so I do not know what I am doing wrong
This is how I have it implemented:

Direction.Normalize ();
object.AddForce (Direction * (Force1 * Force2));

Thank you very much for the help I have been with this for weeks and I can not solve it alone

Note that vector.Normalize (); only works if vector is a field or local variable, not if it’s a property. In that case, you’d have to use vector = vector.normalized;.

For your example to not apply a force, either Force1 or Force2 has to be zero. Is this the case?

If both Force1 and Force2 are greater than 0 and you don’t want to apply the force, then you don’t call AddForce, e.g.

if (ForceEnabled) {
    target.AddForce(Direction.normalized * Force1 * Force2);
}
1 Like

force1 and force 2 if they must apply force since it is the one that I control, it is only the direction that does not have to apply any.

however I have simplified the function for testing, currently I have it like this:

target.AddForce (Direction.normalized * 1000f);

and the direction continues to modify the force in which the target comes out

I still don’t understand what you’re trying to do. Are you just trying to change the direction that an object is moving in without changing the speed?

That’s how it’s supposed to work. The 1000f controls the sped. As long as the vector has a length of one, which is guaranteed by the normalization, the effective force will be 1000 along the direction.

As @kdgalla mentions, you probably need to tell us a bit more about what you’re trying to achieve, maybe the issue is not with the direction.

Another note about AddForce, its force mode is also important. The default is ForceMode.Force, which basically means “force applied over time, accounting for weight” and requires you to call AddForce over multiple frames. ForceMode.Impulse means “force applied once, accounting for weight” and should only be called once. (ForceMode.Acceleration and ForceMode.VelocityChange are analogous to the two but ignore weight.)

Basically the problem is that when I put a direction it always puts a force on me, and since it is a random direction the force implied by the direction is also random, but I need to have the force the object is going to be controlled but having the random direction I the random force comes out too

with this:

target.AddForce (Direction.normalized * 1000f);

what I try is that the force is always 1000 but the direction always random, but it doesn’t work like that and I don’t know how to do it

Maybe with ForceMode.VelocityChange it could work, but until tomorrow I can’t test it and I have to study how to do the ForceMode.VelocityChange because I have never used it

I hope you can understand me and I’m sorry to bother you so much

That should work fine. How are you determining the random direction vector?

this is giving me random direction but also random force, but i need fixed force

How often are you randomly changing the direction? If you do it too quickly, you’ll end up applying lots of random small forces that will add up to a resulting random force that will vary in strength, because some of the small forces randomly cancel each other out.

Try to only set a random direction at start and then don’t change it anymore, you should see the object move at the same speed in a random direction each time you play.

Well now I think you have understood me :slight_smile:

the random direction does not change the frequency very fast since it changes every time the player makes a specific movement with the mouse.

If I don’t change the address the problem does not exist, what happens is that it is necessary to change the address every time the player wants

basically I need an object to always move at the same speed but in random directions when the player wants

I’m sorry to explain myself so badly but I don’t know much English

The code you’ve posted does apply a fixed force in a given direction. If you get a different result, then something else in your code has to be wrong. Did you try logging out Debug.Log(Direction.normalized * 1000f); to make sure the force vector doesn’t change when you don’t expect it to?

Applying a force can also be pretty slow to take a visible effect, depending on the drag, mass and strength of the force. You could try doing target.velocity = Direction.normalized * 1000f; to override the velocity and produce an immediate result, which can give you a hint on what could be wrong.

Also, you need to call AddForce with ForceMode.Force (the default) in FixedUpdate. Calling it in other places can lead to the force not being applied correctly. ForceMode.Impulse and ForceMode.VelocityChange can be called from anywhere but shouldn’t be called every frame.

If that’s the case, why not just forget addForce and set the velocity directly. You know you can just set the velocity to whatever you want, right?

Adding the same force does not mean the object will move at the same speed. For example if you add a force in same direction that the object is moving then it will start moving faster then it did before. If you add a force in opposite direction, the object will slow down. It may reverse direction if the force is strong enough but it will be slower than if the force was applied in a different direction.

Edit: By the way, if you still want to use force to maintain a constant speed, then you need something like a PID controller. I mentioned above that the same force does not always result in the same speed, so that means you need a formula to calculate how much force is necessary to maintain the same speed. That’s what a PID controller does, essentially. If you google “Unity PID Controller” you’ll see a lot of articles and implementations, like this: http://luminaryapps.com/blog/use-a-pid-loop-to-control-unity-game-objects/

I have understood that in my case it is better to apply speed with target.velocity and not force with addforce so I have changed it and I have it like this: target.velocity = Direction.normalized * 100f

but this way I keep putting the random force.

I have put as you have indicated Debug.Log (Direction.normalized * 1000f);
and I have started the script 2 times:

  1. (59.4, 30.3, 997.8)
  2. (125.06, 382.7, 915.3)

I have also applied the .Normalize and not the .normalized but although they give different results, the speed is still random

kdgalla I really don’t want a constant force I just want to be able to control it myself without the variations that the direction applies to me

We don’t know what exactly you’re trying to do and so we can only guess what the right approach would be. Try to create a simple script that illustrates your problem and post it here.

Both of these vectors have a length of 1000. They will apply the same force/velocity but in two different directions.

How do you determine the speed is random?

I mentioned earlier that Normalize() can not work in some situations. But when applied correctly, it will give the same result as normalized.

I can’t explain it better because of my bad English.

I only need an object to be applied a random direction and a force / speed that I indicate, without the direction modifying the speed / force that I am giving it.

1.(59.4, 30.3, 997.8)
2.(125.06, 382.7, 915.3)
These two vectors are not applying the same force / speed because in the second vector the object moved much further away than in the first vector that hardly moved from the site

the speed is determined by the player in each jump the player applies a different speed and the direction is determined by the player’s hand in VR

Setting the velocity should work directly.

target.velocity = Direction.normalized * 10f

You can set your direction to be whatever you want, and it will move at 10 units/second in that direction.