Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Send GameObject to another script

Discussion in 'Scripting' started by Roooomale, May 12, 2020.

  1. Roooomale

    Roooomale

    Joined:
    May 12, 2020
    Posts:
    1
    Hello!
    I have 3 scripts:
    AAMLauncher.cs - creates air-to-air missiles (AAM), attached to Player
    AimingCamera.cs - find enemy targets, attached to aiming camera and AAM prefab
    HomingMissile.cs - homing AAM to the enemy target, , attached to AAM prefab
    How can I send GameObject (enemy target) from AimingCamera.cs to HomingMissile.cs?

    AAMLauncher.cs:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AAMLauncher : MonoBehaviour
    4. {
    5.     public Rigidbody AAM;
    6.     public Transform AAMPoint;
    7.     public int AAMSpeed = 100;
    8.     public float LaunchDelay = 2f;
    9.     public float Damage = 10f;
    10.     public string[] targetTags = { "Ground", "Building", "Soldier", "Tank", "APC", "NSTV", "Helicopter" };
    11.     private float CurDelay = 0f;
    12.  
    13.     public void AAMLaunch()
    14.     {
    15.         if (CurDelay <= 0f)
    16.         {
    17.             CurDelay = LaunchDelay;
    18.             Rigidbody AAMInstance = Instantiate(AAM, AAMPoint.transform.position, AAMPoint.transform.rotation) as Rigidbody;
    19.         }
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         if (Input.GetMouseButton(0)) AAMLaunch();
    25.         if (CurDelay > 0f) CurDelay -= Time.deltaTime;
    26.     }
    27. }
    AimingCamera.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. public class AimingCamera : MonoBehaviour
    3. {
    4.     public int AngularVelocity = 180;
    5.     private float X, Y, Z;
    6.     private float eulerX = 0, eulerY = 0;
    7.     private Vector3 AimingPoint;
    8.     public GameObject AimTarget;
    9.     void Update()
    10.     {
    11.         X = Input.GetAxis("Mouse X") * AngularVelocity * Time.deltaTime;
    12.         Y = -Input.GetAxis("Mouse Y") * AngularVelocity * Time.deltaTime;
    13.         eulerX = (transform.rotation.eulerAngles.x + Y) % 360;
    14.         eulerY = (transform.rotation.eulerAngles.y + X) % 360;
    15.         transform.rotation = Quaternion.Euler(eulerX, eulerY, 0);
    16.         RaycastHit hit;
    17.         AimingPoint = new Vector3(X, Y, Z);
    18.         if (Physics.Raycast(AimingPoint, transform.rotation * Vector3.forward, out hit))
    19.         {
    20.             AimTarget = hit.collider.gameObject;
    21.         }
    22.     }
    23. }
    HomingMissile.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. public class HomingMissile : MonoBehaviour
    3. {
    4.     public GameObject AimTarget;
    5.     public GameObject AAMExplosionPrefab;
    6.     public float AAMSpeed = 20;
    7.     public float AAMTurnSpeed = 90;
    8.     public float AAMExplosionTime = 20;
    9.     private Transform _thisTransform;
    10.     public void Awake()
    11.     {
    12.         _thisTransform = transform;
    13.     }
    14.     void Update()
    15.     {
    16.         AAMExplosionTime -= Time.deltaTime;
    17.         if (AAMExplosionTime <= 0)
    18.         {
    19.             Explode();
    20.             return;
    21.         }
    22.         Vector3 movement = _thisTransform.forward * AAMSpeed * Time.deltaTime;
    23.         if (AimTarget != null)
    24.         {
    25.             Vector3 direction = AimTarget.transform.position - _thisTransform.position;
    26.             float maxAngle = AAMTurnSpeed * Time.deltaTime;
    27.             float angle = Vector3.Angle(_thisTransform.forward, direction);
    28.             if (angle <= maxAngle) _thisTransform.forward = direction.normalized;
    29.             else _thisTransform.forward = Vector3.Slerp(_thisTransform.forward, direction.normalized, maxAngle / angle);
    30.             float distanceToTarget = direction.magnitude;
    31.             if (distanceToTarget < movement.magnitude)
    32.             {
    33.                 Explode();
    34.                 return;
    35.             }
    36.         }
    37.         _thisTransform.position += movement;
    38.     }
    39.     public void Explode()
    40.     {
    41.         if (AAMExplosionPrefab != null) Instantiate(AAMExplosionPrefab, _thisTransform.position, _thisTransform.rotation);
    42.         Destroy(gameObject);
    43.     }
    44.     public void OnCollisionEnter()
    45.     {
    46.         Explode();
    47.     }
    48. }
    AAM (Clone) inspector:
     

    Attached Files:

  2. HypnotistDK

    HypnotistDK

    Joined:
    Feb 14, 2016
    Posts:
    15
    Getting the script in start with:
    hm = GetComponent<HomingMissile>();

    Save it in a private variable and use and when you find your target:

    hm.AimTarget = AimTarget: