- Home /
AddForce on Prefab has inconsistent behaviour.
I am instantiating a prefab and using AddForce via ForceMode.Impulse to make it move. Unfortunately, every once in a while (approximately 1 in 20), AddForce does not work. Here is the relavent code:
using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(PauseManager))]
public class PlayerConstructor : MonoBehaviour
{
public GameObject playerPrefab;
public GameObject planetLooperPrefab;
public GameObject player;
public Transform startPosition;
public Transform impulseTarget;
public bool planetLooper;
void Awake()
{
player = (GameObject)Instantiate(playerPrefab);
player.name = "Player";
GetComponent<PauseManager>().toPause.Add(player.GetComponent<PauseBehaviour>());
if(startPosition != null)
player.transform.position = startPosition.position;
if(startPosition != null && impulseTarget != null)
player.rigidbody.AddForce(impulseTarget.position - startPosition.position, ForceMode.Impulse);
if(planetLooper && planetLooperPrefab != null)
{
GameObject looper = (GameObject)Instantiate(planetLooperPrefab);
looper.transform.parent = player.transform;
looper.GetComponent<PlayerLooped>().player = player;
looper.GetComponent<MessageBroadcaster>().targets[0] = gameObject;
}
}
}
So far in my troubleshooting I have confirmed:
startPosition and impulseTarget are not null.
AddForce does indeed get called.
The force being added is significant. (it is 1000 on a game object of mass 1)
The object is not colliding with anything (Debug.Log in OnCollisionEnter)
No dice. I double-checked via Debug statements and breakpoints. AddForce is being called and a significant force is being applied. No collision.
I should have read your question more closely given your list at the bottom. You do say that the force being applied is significant. Does that mean you output impulseTarget - startPosition.position
? As a check you could try:
player.rigidbody.AddForce((impulseTarget.position - startPosition.position).normalized * 1000.0, Force$$anonymous$$ode.Impulse);
Same behaviour. I'm now looking at: http://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html
At the bottom, it says physics simulation occurs before rigidbody applies transform and position. $$anonymous$$aybe there is a problem with the force I am applying being cleared if there are multiple physics simulation call(which could easily happen as my script is executing at scene load, and that could create a larger deltaTime). I'm trying to figure out how to test this now.
Answer by MaskedPixel · Jul 15, 2013 at 06:16 PM
I have since moved that AddForce that was causing issues from 'Awake' to 'FixedUpdate'. The problem seems to have disappeared.
This solution confuses me because if you check the docs on Execution Order, Awake is called before FixedUpdate and there is nothing specifying that anything in the Physics changes between this. The Rigidbody docs do specify that all physics functionality should be used within FixedUpdate, but I still don't see why it couldn't be used in Awake.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110143im_/https://answers.unity.com/themes/thub/images/avi.jpg)