GameObject UI(命名对话)即使应该设置为 active 也没有设置为 true

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

在下面的代码中,


    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
        if (collision.gameObject.CompareTag("Stratosphere"))
        {
            SceneManager.LoadScene("Peru");
            inPeru = true;
        }
        if (collision.gameObject.CompareTag("Talk"))
        {
            scourgeTalk.SetActive(true);
        }
        if (collision.gameObject.CompareTag("PredaconSpawn") && cheetorr.dead)
        {
            terrosaur.SetActive(true);
            waspinator.SetActive(true);
            conversation.SetActive(true);
        }
    }

在 PredaconSpawn 碰撞中的 OnCollisionEnter2D 函数的底部,仅比较 terrosaur 和 waspinator SetActive 为 true。虽然谈话没有。 完整代码如下,

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

public class Player : MonoBehaviour
{
    public bool hasTransformed = false;
    [HideInInspector]
    public float horizontal;
    public int speed = 5;
    public int jumpVel = 10;
    public bool isGrounded = true;
    public Rigidbody2D rb;
    public GameObject bodyParts;
    public GameObject bodyParts1;
    public GameObject bodyParts2;
    public Animator anim;
    public int speed1 = 10;
    public BoxCollider2D robotCollider;
    public float timerToChange = 2f;
    public bool decrease = false;
    public bool decrease1 = false;
    public GameObject car;
    public BoxCollider2D carCollider;
    public int ammo = 20;
    public float timerToReload = 3f;
    public GameObject shootParticle;
    public bool decrease2 = false;
    public float timerToParticle = 1.5f;
    public bool particle = false;
    public bool inPeru = false;
    public GameObject scourgeTalk;
    public Animator cheetor;
    public GameManager gm;
    public Cheetor cheetorr;
    public GameObject terrosaur;
    public GameObject waspinator;
    public bool cheetorWalk = true;
    public bool right;
    public GameObject conversation;
    public bool left;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        if (!hasTransformed)
        {
            transform.position += new Vector3(horizontal, 0f, 0f) * speed * Time.deltaTime;
        }else if (hasTransformed)
        {
            transform.position += new Vector3(horizontal, 0f, 0f) * speed1 * Time.deltaTime;
        }
        
        if (!hasTransformed)
        {
            Jump();
        }
        Transform();
        Animate();
        if (cheetorWalk)
        {
            CheetorWalkAnim();
        }
    }
    void CheetorWalkAnim()
    {
        if(horizontal == 0 && gm.done)
        {
            cheetor.SetBool("run", false);
        }
        else
        {
            cheetor.SetBool("run", true);
        }
    }
    void Animate()
    {
        if(horizontal == 0)
        {
            anim.SetBool("run", false);
        }
        else
        {
            anim.SetBool("run", true);
        }

        if (horizontal < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            right = true;
            left = false;
        }

        if (horizontal > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            left = true;
            right = false;
        }
    }
    void Transform()
    {
        if(Input.GetKey(KeyCode.Space) && hasTransformed)
        {
            anim.SetTrigger("retransform");
            decrease1 = true;
            timerToChange = 1.7f;
            hasTransformed = false;
        }
        if (Input.GetKey(KeyCode.T) && !hasTransformed)
        {
            anim.SetTrigger("transform");
            decrease = true;
            timerToChange = 1.7f;
            hasTransformed = true;
        }
        if (decrease)
        {
            timerToChange -= 1 * Time.deltaTime;
        }
        if (decrease1)
        {
            timerToChange -= 1 * Time.deltaTime;
        }
        if(timerToChange <= 0 && hasTransformed)
        {
            bodyParts.SetActive(false);
            bodyParts1.SetActive(false);
            bodyParts2.SetActive(false);
            robotCollider.isTrigger = true;
            hasTransformed = true;
            car.SetActive(true);
            carCollider.isTrigger = false;
        }
        if (timerToChange <= 0 && !hasTransformed)
        {
            bodyParts.SetActive(true);
            bodyParts1.SetActive(true);
            bodyParts2.SetActive(true);
            robotCollider.isTrigger = false;
            hasTransformed = false;
            car.SetActive(false);
            carCollider.isTrigger = true;
        }
    }
    void Jump()
    {
        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            isGrounded = false;
            rb.AddForce(Vector2.up * jumpVel, ForceMode2D.Impulse);
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
        if (collision.gameObject.CompareTag("Stratosphere"))
        {
            SceneManager.LoadScene("Peru");
            inPeru = true;
        }
        if (collision.gameObject.CompareTag("Talk"))
        {
            scourgeTalk.SetActive(true);
        }
        if (collision.gameObject.CompareTag("PredaconSpawn") && cheetorr.dead)
        {
            terrosaur.SetActive(true);
            waspinator.SetActive(true);
            conversation.SetActive(true);
        }
    }

我尝试用不同的 UI 游戏对象替换游戏对象,但没有成功。

c# unity3d collision
© www.soinside.com 2019 - 2024. All rights reserved.