- Home /
Power of Rigidbody.AddForce less on build
Hi, just wondering why it seems like something is affecting my rigidbody.addforce on a simple grab and throw script. When I am running the game in the editor, I throw much harder that when I have built the game. Here is my code below:
public class Grab : MonoBehaviour
{
public LayerMask layerMask;
public float reach = 4f;
public Rigidbody grabPos;
public bool isConnected = false;
Rigidbody connectedTo;
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, reach, layerMask) && Input.GetMouseButtonDown(0) && !isConnected)
{
isConnected = true;
connectedTo = hit.collider.GetComponent<Rigidbody>();
}
else if (Input.GetMouseButtonDown(0))
{
if (isConnected)
{
isConnected = false;
connectedTo.transform.parent = null;
connectedTo.isKinematic = false;
connectedTo.AddForce(Camera.main.transform.forward * 100000 * Time.deltaTime);
connectedTo.AddTorque(new Vector3(Random.Range(-50,50), Random.Range(-50, 50), Random.Range(-50, 50)));
connectedTo = null;
}
}
if (isConnected && Vector3.Distance(connectedTo.transform.position,grabPos.transform.position) < 0.3f)
{
connectedTo.transform.parent = Camera.main.transform;
connectedTo.isKinematic = true;
}
else if (isConnected)
{
connectedTo.GetComponent<Rigidbody>().AddForce((grabPos.transform.position - connectedTo.transform.position) * 3000 * Time.deltaTime);
}
}
}
All help is hugely appreciated, let me know if you need anything else like code etc.
Answer by Bunny83 · Jun 09, 2020 at 10:44 AM
Another case of wrongly used deltaTime ^^. DeltaTime is used to reduce / counteract the effect of different framerates on continuous processes over time. For any one-time-events the usage of deltaTime makes no sense at all. It essentially gives you the exact opposite and make that one-time-event being framerate dependent.
At a higher framerate the amount of force you apply would be smaller. This makes sense if you apply that force every frame so after one second you have accumulated the same amount of force, no matter the framerate.
However one-time-events is just a single call of AddForce during one frame. So deltaTime just makes no sense. So when you add your force during a GetMouseButtonDown event, don't use deltaTime.
The next issue is you also apply a continuous force if you're connected and not close enough yet. Here you would have to use deltaTime if you execute this in Update. However continuous forces should be applied in FixedUpdate which has a constant call count per second to ensure the same behaviour and being in sync with the physics update.
Note: Do not move your GetMouseButtonDown code to FixedUpdate. It won't work there properly. Apply one-time-impulses inside Update and any continuous forces in FixedUpdate. You also may want to have a look at the different ForceModes because a one time impulse is not really a force. Using ForceMode.Impulse will actually require much smaller numbers which are equivalent to a sudden change in kinetic energy.
Ok, that makes perfect sense. Thank you very much, I have got it working now!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Rotating prefab when "created" with mousePosition 1 Answer
How to make game moddable? 1 Answer
Springjoint not being added via script. 0 Answers