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)