如何在团结2D中移动和创建精灵?

问题描述 投票:1回答:1

嘿家伙我试图用简单的代码拍摄我有2个C#Classes一个玩家动作和一个子弹

这是Bullet Collision Class

void Start () {
    source.clip = clip;
    bullet = GetComponent<GameObject>();
    rb = GetComponent<Rigidbody2D>();
    bulletPos = player.position;
}

// Update is called once per frame
private void OnTriggerEnter2D(Collider2D wallCol)
{
    if (wallCol.gameObject.tag == "Wall")
    {
        Debug.Log("Wall Hited!");
        source.Play();
        Destroy(bulletPrefab,clip.length);
        if (bullet == null)
            Instantiate(bulletPrefab, bulletPos, Quaternion.identity);
    }
}
public void shoot()
{
    rb.velocity = rb.transform.right * bulletSpeed;
}

这是玩家运动类:

void Update()
{

    if (Input.GetKeyDown(KeyCode.Mouse0) && haveGlock == true)
    {
        bc.shoot();
        AudioSource.PlayOneShot(GlockClip);
    }

}

我确实在另一个类上使用了shoot方法,当方法调用它时,我将对象引用设置为未设置为对象的实例。我还将所需公共变量中的对象拖放到统一中,但为什么它不起作用?

对不起我的坏英国人。

c# windows unity3d
1个回答
0
投票

确保在Player Movement Class中分配了“bc”。

我会在玩家运动班上做类似的事情。

void Update()
   {
    if (Input.GetKeyDown(KeyCode.Mouse0) && haveGlock == true)
    {
        BulletCollision bc = Instantiate(bulletPrefab, bulletPos, Quaternion.identity);
        bc.shoot();
        AudioSource.PlayOneShot(GlockClip);
    }
© www.soinside.com 2019 - 2024. All rights reserved.