[英]How to make this gameobject instantiated as a child of a parent gameobject be positioned at an offset?
我已經按照 Unity 教程中的 C# 創建了一個簡單的 Space Invaders 游戲。 我現在試圖了解正在使用的不同功能。
有一個名為 PlayerController 的類。 它還定義了一個射擊游戲對象,一個由子彈預制件提供的字段:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Transform player;
public float speed;
public float maxBound, minBound;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
// Start is called before the first frame update
void Start()
{
player = GetComponent<Transform> ();
}
// Update is called once per frame
void FixedUpdate()
{
float h = Input.GetAxis ("Horizontal");
if (player.position.x < minBound && h < 0)
h = 0;
else if (player.position.x > maxBound && h > 0)
h = 0;
player.position += Vector3.right * h * speed;
}
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
}
子彈游戲對象使用了 BulletController 類:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour
{
private Transform bullet;
public float speed;
// Start is called before the first frame update
void Start()
{
bullet = GetComponent<Transform>();
}
void FixedUpdate()
{
bullet.position += Vector3.up * speed;
if (bullet.position.y >= 10)
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
Destroy(other.gameObject);
Destroy(gameObject);
PlayerScore.playerScore++;
}
else if (other.tag == "Base")
Destroy(gameObject);
}
}
據我了解,Transform 對象具有位置、旋轉和縮放的值。 那么首先,什么是聲明 x = GetComponent; 做?
其次,“shotSpawn”從哪里獲取它的值? 從代碼應用到的對象?
第三,子彈在作為玩家船體的正方形的中心被實例化,但我希望它在 y 軸上開始更高,所以它從大炮形狀的末端開始。 它似乎也與船體在圖形上相交,所以我想將它稍微移動到 z 軸。 那你怎么能寫呢? 我嘗試添加 shotSpawn.position 的值,但它不斷聲明錯誤。
提前致謝。
GetComponent
意味着基本上只是抓取對象的一個組件。 舉個簡單的例子: Main Camera
有很多組件: 1.1. 一個Transform
組件。 如您所知,此組件用於定義對象的位置、旋轉和縮放
1.2. 一個Camera
組件。 這有很多領域
1.3. 一個Flare Layer
組件。
等等。
這些組件可以通過腳本獲取。 開發人員使用它的原因是為了他們的屬性。 例如,通過說Transform playerTransformComp = player.GetComponent<Transform>();
,您將能夠編寫playerTransformComp.position
。 position
是Transform
類型對象的屬性。
我不認為您在教程中看到的GetComponent
有用,因為無論如何每個游戲對象都有一個變換組件,所以如果他們將玩家聲明為public GameObject player;
,然后使用player.transform.position
代替,它會更容易。 事實上,我認為將某些東西聲明為Transform
,然后獲取Transform
組件甚至沒有任何意義。 正如@BugFinder 在您之前的帖子中所說,該教程總體上非常糟糕。
shotSpawn
的值來自...本身! 它是一個公共對象,所以我假設您將shotSpawn
對象從場景中拖放到腳本的字段中。 這意味着腳本中的shotSpawn
對象是您在腳本字段上拖放的對象。 您可以使用其所有功能,拖放的對象將受到影響。 因此,您可以使用shotSpawn.position
和shotSpawn.rotation
。 我可能會在這里重復一下,但請注意shotSpawn
是一個Transform
對象,因此您可以使用典型的Transform
對象的屬性。
文檔上Transform.position
(以及在一個Transform.rotation
說你要使用Vector3
對象來值。減去添加或給他們。
一個人會做shotSpawn.position + new Vector3(10f, 5f, 10f)
。 當然,你也可以這樣做
value = new Vector3(10f, 5f, 10f);
然后Instantiate(shot, shotSpawn.position + value, shotSpawn.rotation);
另外,請(為了將來),嘗試在每個帖子中提出一個問題,否則人們會忽略您的問題甚至標記它並將其刪除。 我曾經和你一樣,所以我不會那樣做,但是在發表進一步的帖子時請考慮到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.