Unity中如何在2D中指向特定物体的中心坐标?
How to point to the center coordinates of a specific object in 2D in Unity?
提问前声明我是初学者
如有不妥之处,敬请谅解
我会用图片和代码来说明我要实现的东西
下面是图片。
如上图所示,我已经实现了在 5 到 7 个随机位置之间生成的当前导弹(预制件)。
这是相关代码:
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)
{
}
}
我的问题从这里开始。
用随机坐标创建的导弹对象的Z轴值为零。 (旋转 Z)
我希望导弹(预制件)指向Center_Planet的中心坐标。
例如下图所示。
为了实现这一点,我使用了 LookAT 函数,也尝试过使用 Quaternion.rotation 函数。
但是执行失败了。
SO..为了实现以上,我们尝试使用Mathf函数进行计算,我不知道如何公式化。
为了实现上述目标,我将不胜感激有关如何设置公式的一些建议。
如果你能给我作为初学者的建议,我将不胜感激。
您可以使用 transform.right = ....
分配二维元素的 "forward" 矢量
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);
// ...
提问前声明我是初学者
如有不妥之处,敬请谅解
我会用图片和代码来说明我要实现的东西
下面是图片。
如上图所示,我已经实现了在 5 到 7 个随机位置之间生成的当前导弹(预制件)。
这是相关代码:
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)
{
}
}
我的问题从这里开始。
用随机坐标创建的导弹对象的Z轴值为零。 (旋转 Z)
我希望导弹(预制件)指向Center_Planet的中心坐标。
例如下图所示。
为了实现这一点,我使用了 LookAT 函数,也尝试过使用 Quaternion.rotation 函数。
但是执行失败了。
SO..为了实现以上,我们尝试使用Mathf函数进行计算,我不知道如何公式化。
为了实现上述目标,我将不胜感激有关如何设置公式的一些建议。
如果你能给我作为初学者的建议,我将不胜感激。
您可以使用 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);
// ...