Question by
jinology · Apr 25, 2016 at 10:16 AM ·
instantiatereferencingarray of gameobjects
I am trying to access transform[] and count from SCRIPT A, however, its not working perfectly. SCRIPT B didnt apply the effect onto the instantiated fish from SCRIPT A. Only some fishes fly to the targets.
SCRIPT A
using UnityEngine; using System.Collections;
public class NemoTarget1 : MonoBehaviour {
public GameObject targetMesh ;
public GameObject[] targetspot ;
public int count;
public Transform[] targetbody;
void Start ()
{
int count = targetspot.GetLength(0);
targetspot = new GameObject [count];
}
}
////////////////////////////////////////////////////////////////////// SCRIPT B
using System.Collections;
public class projectile3 : MonoBehaviour {
public Transform[] target = new Transform[6];
public int n ;
// private float firingAngle = 45.0f;
public float gravity = 900f;
public Transform Projectile;
private Transform myTransform;
public float Xlimit = -180f;
void Awake()
{
myTransform = transform;
}
void Start()
{
// GameObject targetcount = GameObject.Find ("Nemo_spawner");
// NemoTarget nemotarget = targetcount.GetComponent<NemoTarget> ();
// int n = nemotarget.count;
GameObject fishcount = GameObject.Find ("Nemo_spawner");
FishSpawnPoint nemotarget1 = fishcount.GetComponent<FishSpawnPoint> ();
nemotarget1.count = n;
// GameObject targetbody = GameObject.Find ("Nemo_spawner"); // FishSpawnPoint nemotarget2 = targetbody.GetComponent (); // nemotarget2.TargetPoint = Target;
{
{
{
target [n] = GameObject.Find ("Target" + (n + 1)).transform;
}
StartCoroutine (SimulateProjectile ());
}
}
}
IEnumerator SimulateProjectile()
{
Transform Target; // added codes
GameObject fishcount = GameObject.Find ("Nemo_spawner");
FishSpawnPoint nemotarget1 = fishcount.GetComponent<FishSpawnPoint> ();
nemotarget1.count = n;
target [n] = GameObject.Find ("Target" + (n + 1)).transform;
float yRange = 900f; //changing this value will slow down low angle's moving speed
float yLow = -488f;
float throughY = (Projectile.position.y - yLow) / yRange;
float firingAngle = 50.0f - 40.0f * throughY;
// GameObject[] targetPoints = GameObject.Find ("Target 1");
int targetIndex = Random.Range (0, target.Length); // added codes
Target = target [targetIndex];
yield return new WaitForSeconds(0.2f);
// Move projectile to the position of throwing object + add some offset if needed.
Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
// Calculate distance to target
float target_Distance = Vector3.Distance(Projectile.position, Target.position);
// Calculate the velocity needed to throw the object to the target at specified angle.
float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
// Extract the X Y componenent of the velocity
float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
// Calculate flight time.
float flightDuration = target_Distance / Vx;
// Rotate projectile to face the target.
Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);
float elapse_time = 0;
while (elapse_time < flightDuration)
{
Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
elapse_time += Time.deltaTime;
yield return null;
}
}
}
Comment