Question by
Bionicghost · Mar 07, 2017 at 08:50 PM ·
transformtrackingmissile
Feeding a stored transform to a different script
Greetings.
I'm trying to create a script that shoots seeking missiles to a target that has been locked onto. The script shoots a raycast when the fire buton is held, and stores the transform of the first gameobject with the 'enemy' layer it encounters as a public variable "target". Then, when the fire button is released, it should instantiate a missile prefab, give the script that controls it the transform of the enemy, then delete the transform to prepare for the next shot.
So my question is, how do i make the missile shooter script feed the target's transform to the missile?
Here are the scripts so far:
public class MissileShooter : MonoBehaviour {
public GameObject missile;
public Transform cannonEnd;
public Transform target;
public LayerMask layerMask;
public bool isTargetSet=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Square")&&!isTargetSet)
{
RaycastHit lockOn;
if (Physics.Raycast(transform.position, /*fireRotation * */ Vector3.forward, out lockOn, Mathf.Infinity, layerMask))
{
//check how to add a timer to see if the enemy gets out of the camera
target = lockOn.transform;
isTargetSet = true;
}
}
if (Input.GetButtonUp("Square")&&isTargetSet){
Instantiate(missile,cannonEnd.position, cannonEnd.rotation);
target = null;
isTargetSet = false;
}
}
}
and
public class MissileMover : AutomaticDestroyObject {
public float missileSpeeed = 30;
public Transform target;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update () {
transform.LookAt(target);
transform.localPosition += transform.forward * Time.deltaTime * missileSpeeed;
}
}
Comment