I have been trying to get a clone of my ball game object to have force applied to it upon spawning, but I haven’t been able to figure out how to fit it into my code. Speaking of which, here is my code:
using UnityEngine;
using System.Collections;
public class Spawning : MonoBehaviour
{
public GameObject ball;
public Rigidbody rb;
public float spawnRate;
public int ballCount;
public float speed;
public Vector2 movement;
void Awake () {
rb = GetComponent<Rigidbody> ();
}
void Start () {
while(ballCount >= 0 && spawnRate <= 10)
InvokeRepeating ("SpawningClones", 5, 2);
}
void SpawningClones (){
Instantiate (ball, new Vector3 (UnityEngine.Random.Range (-2.22f, 2.22f), UnityEngine.Random.Range (3.8f, 4.8f)), Quaternion.identity);
}
void FixedUpdate () {
spawnRate += Time.deltaTime;
Vector2 movement = new Vector2 (UnityEngine.Random.Range (-10 * speed, 10 * speed), UnityEngine.Random.Range (-10 * speed, 10 * speed));
spawnRate = 0;
ballCount += 1;
}
}
//this is what I would like to put into my code, but I don't know where. I tried putting it into the FixedUpdate method, but that didn't work: rb.AddForce (movement * 1);