Search code examples
c#unity-game-enginemath-functions

How to point to the center coordinates of a specific object in 2D in Unity?


Before questioning, I declare that I am a beginner.

Please understand that there may be a lack of explanation.

I'll explain what I want to implement with pictures and code.

Below is a picture.

enter image description here

Like the picture above, I have implemented the present missile (prefab) to be generated between 5 and 7 random locations.

Here's the code for that:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

class MissileAnimation : WordGameSingleton<MissileAnimation>
{
    private int missile_num = 0; 
    public List<GameObject> missile_list = new List<GameObject>();
    float fixedPosition_z = 1f; 
    public GameObject missile_parent_area = null; 

    protected override void Start()
    {
        StartCoroutine(fireMissiles());

        missile_parent_area = new GameObject();
        missile_parent_area.name = "MissileAnimation"; 
    }

    protected override void Update()
    {
    }

    private IEnumerator fireMissiles()
    {
        yield return new WaitForSeconds(0.3f);


        missile_parent_area.transform.SetParent(this.gameObject.transform);
        missile_parent_area.transform.localScale = new Vector3(1f, 1f, 1f);
        missile_parent_area.transform.localPosition = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, fixedPosition_z);
        //GameObject planet_position = GameObject.Find("Center_Planet") as GameObject;position.x
        //float planet_position_x = planet_position.transform.position.x;
        //float planet_position_y = planet_position.transform.position.y;

        missile_num = UnityEngine.Random.Range(5, 7); // Generate between 5 and 7EA missiles
        int origin_width = 50;
        int origin_height = 100;

        for (int i = 0; i < missile_num; ++i)
        {
            // Generate missile position random position
            float angle = Mathf.Round(UnityEngine.Random.Range(90f, 180f) + 45f);
            float randomPosition_x = Mathf.Cos(angle) * (Screen.width - origin_width) / 2; 
            float randomPosition_y = Mathf.Sin(angle) * (Screen.height - origin_height) / 2;

            // Center view of the Planet
            // How do I implement this part?

            // Create a missile prefab
            GameObject missile_prefab_load = Resources.Load("Prefabs/Object/Missile") as GameObject;
            GameObject missile_prefab_instantiate = Instantiate(missile_prefab_load, this.gameObject.transform.position, Quaternion.identity) as GameObject;
            //    MonoBehaviour.Instantiate(Resources.Load("Prefabs/Object/Missile"), new Vector3(0f, 0f, 0f), Quaternion.identity) as GameObject;
            missile_prefab_instantiate.name = "missile_" + i;
            missile_prefab_instantiate.transform.SetParent(missile_parent_area.transform);
            missile_prefab_instantiate.transform.localPosition = new Vector3(randomPosition_x, randomPosition_y, 0f);
            missile_prefab_instantiate.transform.localScale = new Vector3(2.0f , 2.0f, 2.0f);
            missile_list.Add(missile_prefab_instantiate);


            // Speed ​​of missile flight
            float speed = UnityEngine.Random.Range(1.0f, 1.4f);

            // Go Missile
            LeanTween.moveLocal(missile_prefab_instantiate, new Vector3(0f, 0f, 0f), speed).setEase(LeanTweenType.easeInCubic).setOnUpdate(crashPlanet).setOnUpdateParam(missile_prefab_instantiate);
            // LeanTween.moveLocal(GameObject , Vector , time:float)
            // setOnUpdate : <float,Object> 

            // Reduced size as the rocket fires.
            LeanTween.scale(missile_prefab_instantiate, new Vector3(1.0f, 1.0f, 1.0f), speed).setEase(LeanTweenType.easeInCubic);
        }
        yield return new WaitForSeconds(0.9f);

        Vector3 position = this.gameObject.transform.localPosition;
        this.transform.position = new Vector3(position.x, 0f, position.z);
    }

    private void crashPlanet(float obj, object missile)
    {

    }
}

My question starts from here.

The value of the Z-axis of a missile object created with random coordinates is zero. (Rotation Z)

I want the missiles (prefabs) to point to the center coordinates of the Center_Planet.

For example, as shown in the picture below. enter image description here

To implement this, I used the LookAT function, also tried using the Quaternion.rotation function.

But the implementation failed.

SO..In order to implement the above, we try to do calculations using the Mathf function, I don't know how to formulate it.

To implement the above, I would appreciate some advice on how to set up the formula.

I would really appreciate it if you could give me advice as a beginner.


Solution

  • You have the possibility to assign the "forward" vector of your 2D element using transform.right = ....

    Transform target = GameObject.Find("Center_Planet").transform ;
    
    // ...
    
    for (int i = 0; i < missile_num; ++i)
    {
        // ...     
         missile_prefab_instantiate.transform.right = target.position - missile_prefab_instantiate.transform.position ;
    }
    yield return new WaitForSeconds(0.9f);
    
    // ...