C# 如何将此游戏对象实例化为父游戏对象的子对象,并将其定位在偏移位置?

C# 如何将此游戏对象实例化为父游戏对象的子对象,并将其定位在偏移位置?,c#,unity3d,C#,Unity3d,我遵循了一个C#in Unity教程创建了一个简单的空间入侵者游戏。我现在试图理解正在使用的不同函数 有一个叫做PlayerController的类。它还定义了一个射击游戏对象,一个与子弹预制一起提供的字段: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private Trans

我遵循了一个C#in Unity教程创建了一个简单的空间入侵者游戏。我现在试图理解正在使用的不同函数

有一个叫做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);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为
{
私有变换播放器;
公众浮标速度;
公共浮点数maxBound、minBound;
公众游戏物体射击;
公共产卵;
公共浮式灭火器;
私人浮动nextFire;
//在第一帧更新之前调用Start
void Start()
{
player=GetComponent();
}
//每帧调用一次更新
void FixedUpdate()
{
float h=Input.GetAxis(“水平”);
if(player.position.xmaxBound&&h>0)
h=0;
player.position+=Vector3.right*h*速度;
}
无效更新()
{
if(Input.GetButton(“Fire1”)和&Time.Time>nextFire)
{
nextFire=时间。时间+燃速;
实例化(快照、shotpawn.position、shotpawn.rotation);
}
}
}
bullet游戏对象使用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);

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类BulletController:MonoBehavior
{
私有转换子弹;
公众浮标速度;
//在第一帧更新之前调用Start
void Start()
{
bullet=GetComponent();
}
void FixedUpdate()
{
bullet.position+=矢量3.up*速度;
如果(项目符号位置y>=10)
摧毁(游戏对象);
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
if(other.tag==“敌人”)
{
销毁(其他游戏对象);
摧毁(游戏对象);
PlayerScore.PlayerScore++;
}
else if(other.tag==“Base”)
摧毁(游戏对象);
}
}
据我所知,变换对象具有位置、旋转和缩放值。 首先,声明x=GetComponent意味着什么;是吗

第二,“shotpawn”的值来自哪里?从应用代码的对象开始

第三,子弹在作为玩家飞船主体的正方形的中心被实例化,但我希望它从y轴的更高位置开始,所以它从加农炮形状的末端开始。它似乎也与船体相交,所以我想把它稍微移到z轴上。那你怎么写呢?我尝试添加shotpawn.position的值,但它一直声明错误

提前谢谢

  • 一个
    GetComponent
    基本上意味着只需要抓取一个对象的组件。举个简单的例子:
    主摄像头
    有很多组件:
  • 1.1。一个
    变换
    组件。正如您所理解的,此组件用于定义对象的位置、旋转和缩放

    1.2。一个
    摄像机
    组件。这有很多领域

    1.3。A
    Flare层
    组件

    等等

    这些组件可以通过脚本抓取。开发人员使用它的原因是为了他们的属性。例如,说
    transformplayertransformcomp=player.GetComponent(),您将能够编写
    playerTransformComp.position
    <代码>位置
    是类型为
    变换
    的对象的属性

    我不认为你在教程中看到的
    GetComponent
    有用,因为每个游戏对象都有一个转换组件,所以如果他们将玩家声明为
    公共游戏对象玩家
    ,然后使用
    player.transform.position
    ,这样会更简单。事实上,我认为将某个东西声明为
    Transform
    ,然后获取
    Transform
    组件是没有意义的。正如@BugFinder在你之前的帖子中所说的,这个教程总体上相当糟糕


    shotpawn
    从。。。它本身它是一个公共对象,因此我假设您将
    shotpawn
    对象从场景拖放到脚本的字段中。这意味着脚本中的
    shotpawn
    对象就是您在脚本字段中拖放的对象。您可以使用其所有功能,拖放的对象将受到影响。因此,您可以使用
    shotpawn.position
    shotpawn.rotation
    。我可能会在这里重复一下,但请注意,
    shotpawn
    是一个
    Transform
    对象,因此可以使用典型的
    Transform
    对象的属性


    Transform.position
    (以及
    Transform.rotation
    上的对象)上,您必须使用
    Vector3
    对象向其添加或减去值

    你可以做
    shotpawn.position+newvector3(10f,5f,10f)
    。 当然,你也可以这样做

    value = new Vector3(10f, 5f, 10f);
    
    然后
    实例化(shot,shotpawn.position+value,shotpawn.rotation);


    另外,请(为了将来),试着在每篇文章中问一个问题,否则人们会忽略你的问题,甚至会将其标记,并将其删除。我曾经和你一样,所以我不会这样做,但请在发表进一步的文章时考虑到这一点。

    创建一个子角色到玩家的飞船。在你的情况下,将子角色转换移动到你想要产生子弹的地方在《佳能》的结尾。然后点击你拥有playerController的游戏对象。然后将你创建的孩子拖到Inspector的shotSpawn。所以你是说玩家可以用更少的步骤获得同样的结果