简体   繁体   中英

how to move and recreate sprite in unity2D?

hey guys im trying to shoot with simple code i have 2 C# Classes one for Player movements and one for Bullet

this is 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;
}

this is Player Movement Class:

void Update()
{

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

}

i did use shoot method on another class and when the method called its show me the object reference not set to instance of the object. also i drag and drop objects in required public variables in unity but why its not gonna work?

sorry for my bad English guys.

Make sure "bc" is assigned in Player Movement Class.

I would do something like that on Player Movement Class.

void Update()
   {
    if (Input.GetKeyDown(KeyCode.Mouse0) && haveGlock == true)
    {
        BulletCollision bc = Instantiate(bulletPrefab, bulletPos, Quaternion.identity);
        bc.shoot();
        AudioSource.PlayOneShot(GlockClip);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM