有什么办法可以限制生成的实例数量吗?

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

我正在统一开发这个游戏,到目前为止一切正常,但是当我销毁一个游戏对象(没有装备的角色)并为带有剑和火炬的角色实例化一个预制件时,它会产生两个预制件而不是一个。

这是我正在使用的代码(对不起,如果它看起来很乱,这是我的第一个项目):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementNoSword : MonoBehaviour
{
    private float move;

    [SerializeField] private bool TorchSwordBool = false;
    [SerializeField] private Transform TorchSword;
    [SerializeField] private GameObject CharacterSword;

    [SerializeField] private float moveSpeed = 3f;

    [SerializeField]private bool jumping; 
    [SerializeField] private float jumpSpeed = 4.8f;

    [SerializeField] private float ghostJump;

    [SerializeField] private bool isGrounded;
    public Transform feetPosition;
    [SerializeField] private Vector2 sizeCapsule;
    [SerializeField] private float angleCapsule = -90;
    public LayerMask whatIsGround;

    Rigidbody2D rb;
    SpriteRenderer sprite;
    Animator animationPlayer;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        sprite = GetComponent<SpriteRenderer>();
        animationPlayer = GetComponent<Animator>();

        sizeCapsule = new Vector2 (0.302f, 0.01f);
    }

    // Update is called once per frame
    void Update()
    {
        if(TorchSwordBool == false)
        {
            //Detect Ground
            isGrounded = Physics2D.OverlapCapsule(feetPosition.position, sizeCapsule, CapsuleDirection2D.Horizontal, angleCapsule, whatIsGround);

            //Player's movement input
            move = Input.GetAxis("Horizontal");

            if (move !=0) {
                moveSpeed += 15f * Time.deltaTime;

                if (moveSpeed >= 3.0f)
                {
                    moveSpeed = 3.0f;
                }
            }
            else
            {
                moveSpeed = 0;
            }

            //Player's jumping input
            if(Input.GetButtonDown("Jump") && ghostJump > 0)
            {
                jumping = true;
            }

            //Flip in the horizontal direction
            if(move < 0)
            {
                sprite.flipX = true;
            }
            else if(move > 0)
            {
                sprite.flipX = false;
            }


            if (isGrounded)
            {
                ghostJump = 0.05f;

                animationPlayer.SetBool("JumpingV", false);
                animationPlayer.SetBool("FallingV", false);
                animationPlayer.SetBool("JumpingH", false);
                //animationPlayer.SetBool("FallingH", false);

                if (rb.velocity.x != 0 && move != 0)
                {
                    animationPlayer.SetBool("Walking", true);
                }
                else
                {
                    animationPlayer.SetBool("Walking", false);  
                }
            }
            else
            {
                ghostJump -= Time.deltaTime;

                if(ghostJump <= 0)
                {
                    ghostJump = 0;
                }   

                if (rb.velocity.x == 0)
                {
                    animationPlayer.SetBool("Walking", false);

                    if (rb.velocity.y > 0)
                    {
                        animationPlayer.SetBool("JumpingV", true);
                        animationPlayer.SetBool("FallingV", false);
                        animationPlayer.SetBool("JumpingH", false);
                        //animationPlayer.SetBool("FallingH", false);
                    }
                    if (rb.velocity.y < 0)
                    {
                        animationPlayer.SetBool("JumpingV", false);
                        animationPlayer.SetBool("FallingV", true);
                        animationPlayer.SetBool("JumpingH", false);
                        //animationPlayer.SetBool("FallingH", false);
                    }
                }
                else
                {
                    if (rb.velocity.y > 0)
                    {
                        animationPlayer.SetBool("JumpingV", false);
                        animationPlayer.SetBool("FallingV", false);
                        animationPlayer.SetBool("JumpingH", true);
                        //animationPlayer.SetBool("FallingH", false);
                    }
                    if (rb.velocity.y < 0)
                    {
                        animationPlayer.SetBool("JumpingV", false);
                        animationPlayer.SetBool("FallingV", true);
                        animationPlayer.SetBool("JumpingH", false);
                        //animationPlayer.SetBool("FallingH", true);
                    }
                }   
            } 
        }
        else
        {
            animationPlayer.SetBool("Walking", false);
            animationPlayer.SetBool("PickST", true);
        }


    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(1, 0, 0, 0.5f);
        Gizmos.DrawCube(feetPosition.position, sizeCapsule);
    }

    void FixedUpdate()
    {
        if(TorchSwordBool == false)
        {
            //Player's movement
            rb.velocity = new Vector2(move * moveSpeed, rb.velocity.y);

            //Player's jump
            if(jumping)
            {
                rb.velocity = Vector2.up * jumpSpeed;
                //rb.AddForce (new Vector2 (0f, jumpSpeed), ForceMode2D.Impulse);

                jumping = false;
            }
        }
    }

    void OnTriggerStay2D(Collider2D Coll)
    {
        if(Coll.gameObject.tag == "TorchSword" && isGrounded)
        {
            TorchSwordBool = true;
            sprite.flipX = false;
            rb.constraints = RigidbodyConstraints2D.FreezePosition;
            transform.position = TorchSword.position;
        }
    }

    void DestroyTorchNSword()
    {
        Destroy(TorchSword.gameObject);
    }

    void DestroyPlayer()
    {
        Instantiate(CharacterSword, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
        Destroy(gameObject);
    }
}
c# unity3d 2d game-development
© www.soinside.com 2019 - 2024. All rights reserved.