Why does the variable need multiplication in addforce?

Hey!

So I’m really, really new at this, and I’ve started to experiment around and fiddle with this and that in Unity. Which brings me to two conundrums.

  1. Right now I’m adding force to a cube, and it works like a charm when multiplied. But I wonder - why can’t I just add a variable with 20000f from the beginning, instead of it working only when multiplied? I’m just trying to wrap my head around it. Mostly in order to understand. :slight_smile:

{
public Rigidbody rb;
public float myforce=20000f;

void Start ()
{

}

void Update ()
{
if (Input.GetKey (“space”))
{

rb.AddForce(Vector3.right * myforce); //this is my strength - not multiplied
Debug.Log(myforce + Time.deltaTime);

}
else
{

}
}
}

  1. Also, what would be the best way to set a variable to a set force that doesnt change over time.?

Have a good sunday you all. :), I apologize if my questions seems quite basic and dumb.

Hi and welcome!
Your force is a float. You cannot just apply a number to an object as force. What would that look like? A force would be applied from below, from left, right, up, or any combination of those directions. Or in other words, a force is a vector of some length. Your force is just a number, so to make any sense you need to multiply it with the direction in which you want to apply the force; in your case Vector.right. Just as an example, if you multiplied it with Vector.left, the force would be applied in the other direction. Thus just calling AddForce(myForce) would have no meaning on its own.

Hope this makes sense. Also, you can use code tags <> to add code. Makes it way more readable!

Hi Yoreki! And thanks for welcoming me. :slight_smile:

Yeah I’m kinda (a little bit) understanding the part of Vector and the need of directions when applying a force. However (lemme see if I can use the codesnippets). - if you see the part where I’ve written “public float myforce = 200f” - how come the 200 isnt kept? I’m doing it wrong, since it’s obviously not working. But why cant the float saved (200f) not be used in Vector3.right * myforce?. Isn’t it the same as Vector3.right * 200f? Just the use of the variables information instead of writing the actual number.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class experimentwithatom : MonoBehaviour
{

public float myforce = 200f;
public Rigidbody rb;

void Start ()
{

}

void Update ()
{

if (Input.GetKey (“space”))
{

rb.AddForce(Vector3.right * myforce);

}
else
{

}
}
}

You can use code tags by pressing the <> button above where you are writing these texts’^^. There is also a sticky post on this subforum explaining how to use them, if you need to look it up.

Maybe i misunderstood your problem. Are you saying that

rb.AddForce(Vector3.right *200f);

behaves differently from

rb.AddForce(Vector3.right * myForce); // with myForce = 200f;

when you run it? That’s identical. The only thing i can imagine that causes different behavior, is that you changed the value of myForce in the inpector. No matter what value you assign to myForce, the inspector value overrides that. So if myForce has a value of 1 in the inspector, and you write myForce = 20000f; at the top of your script, it’s still gonna be 1 at runtime. You can check this by using, for example, the following to visualize the value actually represented by myForce:

Debug.Log("myForce is currently: " + myForce);

May that be your problem? Or am i missing the point?

Yoreti: youre my favourite person of the day. It was probably something in the inspector that overrides it (it was all set to 0). Thanks for all the help, I never thought of checking the actual inspector. :smile:

Hmmm… is the actual “*200” part confusing you?

You can always make a vector the basic way: AddForce( new Vector3(200,0,0) ) means 200 on x, 0 on y and z. Vector3.right*200 is the, using 2 shortcuts: Vector3.right is just new Vector3(1,0,0). And “*200” multiplies everything by 200. Since the other’s are 0, is the same as (200,0,0).

Alternately, and this is the exact same thing. write public Vector pushMeForce;. You’ll get 3 slots, which you can set to (200,0,0). Use AddForce(pushMeForce);

You have to multiply with Time.DeltaTime.

A computer running 30fps will add the force 30 times a second.
A computer running 120fps will add the force 120 times a second.

The faster computer will literally speed up your object 4 times faster every second. The *Time.DeltaTime evens things out. The more frames a computer pushes the lower the number will get. (Less time between frames)

If continuous speed is what you are looking for. Maybe using GetKeyDown (for a 1 frame burst) and setting all kinds of friction to zero on the rb will give it a constant speed when not on a surface.

Thanks for the help Owen and Ben. :). Ultimately it was the wrong number applied in the inspector that overrode the number in the variable. Also - I will try adding Time.DeltaTime for the right amount of measure, thanks. :slight_smile:

Maybe. AddForce is a mess – it has 4 modes which all work differently. 2 multiply by Time.deltaTime, and 2 don’t. The terms it uses are for physics students, not game designers. rb.velocity += force; is a more understandable way to push, and is 99% the same.