so the UsingInstantiate script is placed on the rocket? The video doesn’t actually show any of the scripts being placed anywhere except for the Destroy script. I guess we’re just supposed to be psychic and know where they go, lol? I’m so confused.
I didn’t include my whole code because honestly I thought this would just be a syntax problem, but I guess it’s more complicated than that, so here is all my code:
I have a script called fireScript that I have placed on the camera:
using UnityEngine;
using System.Collections;
public class fireScript : MonoBehaviour
{
//PUBLIC VARS;
public float targetFixedTime = 4f;
public static int points = 0;
//FIRE SPHERE FUNCTION
public void fireSphere(float xVect, float yVect, float zVect)
{
//create sphere
GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
//name sphere
sphere.gameObject.name = "projectile";
//scale sphere
sphere.transform.localScale = new Vector3 (0.1F, 0.1F, 0.1F);
//add a rigidbody
Rigidbody spheresRigidBody = sphere.AddComponent<Rigidbody> ();
//add physical properties
spheresRigidBody.mass = 1;
spheresRigidBody.drag = 0;
//move sphere to camera location;
sphere.transform.position = Camera.main.transform.position;
//fire sphere
sphere.rigidbody.AddForce (new Vector3 (xVect, yVect, zVect));
//destroy sphere after 5 seconds
Destroy(sphere.gameObject, 5);
}
public void genTarget()
{
float tarXloc = Random.Range (-3f, 3f);
float tarYloc = Random.Range (-2f, 3f);
float tarZloc = Random.Range (6f, 12f);
//create target
GameObject target = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
//add physical properties
Rigidbody targetRigidBody = target.AddComponent<Rigidbody> ();
targetRigidBody.isKinematic = true;
//add targetColScript to target
target.gameObject.AddComponent<targetColScript> ();
//scale target
target.transform.localScale = new Vector3(1, 0.1F, 1);
//rotate target
target.transform.Rotate(90, 0, 0);
//move sphere to random location;
target.transform.position = new Vector3(tarXloc, tarYloc, tarZloc);
//set target to self destruct
Destroy(target.gameObject, targetFixedTime - 0.2f);
}
void Start()
{
genTarget ();
InvokeRepeating("genTarget", targetFixedTime, targetFixedTime);
}
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
fireSphere(0f,100f,5000f);
}
}
}
Then I have a collision script that isn’t actually placed on anything, but is added to the instantiated target on line 49 of fireScript:
using UnityEngine;
using System.Collections;
public class targetColScript : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "projectile")
{
flingScript.points += 10;
}
}
}
Could you show me how and where you would add the code to instantiate the Explosion particle system upon collision of target and a sphere?