使玩家穿过墙壁(单向墙)

huangapple go评论132阅读模式
英文:

Make player go through walls (one way wall )

问题

我游戏中有一个功能,需要玩家穿过墙壁来解决谜题,我希望玩家只能穿过带有“Puzzle Wall”标签的墙壁一次,然后在时间结束后它将不再起作用(变成普通墙壁)。

如何使其工作(您可以编写新的代码,因为我的代码有2个错误)

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

public class Walls : MonoBehaviour
{
    private BoxCollider boxCollider;
    private bool hasBeenActivated = false;

    void Start()
    {
        Puzzle.OnPuzzleSolved += ActivateWall;                               
        boxCollider = GetComponent<BoxCollider>();
    }

    void Update()
    {
    
    }

    private void OnTriggerExit(Collider other)
    {
        if (hasBeenActivated)
        {
            boxCollider.isTrigger = false;
        }
    }

    private void ActivateWall(string wallTypeToActivate)
    {
        if (!hasBeenActivated && wallTypeToActivate == "Puzzle Wall")
        {
            boxCollider.isTrigger = true;
            hasBeenActivated = true;
        }
    }
}

错误1:我不能将任何内容传递给我的动作
错误2:空引用异常
错误3:玩家可以穿过所有墙壁

我尝试使用OnTriggerEnter/OnTriggerExit,但它给我错误。
我尝试使用碰撞矩阵,但问题依然存在,尽管它有效,但玩家仍然可以穿过墙壁。

英文:

I have a feature in my game were it requires the player to go through walls to slove the puzzle i want the player to only go through the wall that has the tag Puzzle Wall ONLY once then it will no longer work (become a normal wall) after the time end

How can i make it work (you can write a new code since mine has 2 errors )
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class Walls : MonoBehaviour
{

    // This script is for every wall in the game there is 5 types of walls 

    private BoxCollider boxCollider;

    void Start()
    {
        Puzzle.OnPuzzleSolved += ActivateWall(&quot;Puzzle Wall&quot;); // why do i get this error                                
        boxCollider = GetComponent&lt;BoxCollider&gt;();
    }

  
    void Update()
    {
        
    }

    private void OnTriggerExit(Collider other)
    {
        boxCollider.isTrigger = false;

    }


    private void ActivateWall(string wallTypeToActivate)
    {
        if (wallTypeToActivate == &quot;Puzzle Wall&quot;)
        {
            boxCollider.isTrigger = true;

        }
        else if (wallTypeToActivate 
   == &quot; Fake Wall&quot;)
        {
            boxCollider.isTrigger = true;

        }
        else if (wallTypeToActivate 
     == &quot;Puzzle Door&quot;)
        {
            boxCollider.isTrigger = true;

        }
        else
        {
            boxCollider.isTrigger

   = false;
      }
     }
    }

Error 1 i cant pass anything into my action
Error 2 null refrence execption
Error 3 player can go through all walls

I tried to use the on trigger enter / exist but it give me error

I tried using the collison matrix same issue it works but player still can go through it

答案1

得分: 0

我重构了你的代码并修复了错误。
在检视器中确保计时器值大于0。

using System.Collections;
using UnityEngine;

public class Walls : MonoBehaviour
{
    [SerializeField] private float time;

    // This script is for every wall in the game there are 5 types of walls

    private BoxCollider boxCollider;

    void Start()
    {
        boxCollider = GetComponent<BoxCollider>();

        Puzzle.OnPuzzleSolved += ActivateWall;
    }

    private void OnDisable()
    {
        Puzzle.OnPuzzleSolved -= ActivateWall;
    }

    private void Update()
    {

    }

    private void OnTriggerExit(Collider other)
    {
        switch (gameObject.tag)
        {
            // If the tag matches, we disable the collider on exit so the player can't go back
            case "Puzzle Wall":
                boxCollider.isTrigger = false;
                gameObject.GetComponent<Walls>().enabled = false;
                break;
            case "Fake Wall":
                boxCollider.isTrigger = true;
                break;
            case "Puzzle Door":
                boxCollider.isTrigger = true;
                break;
        }
    }

    private void ActivateWall()
    {
        boxCollider.isTrigger = true;
        StartCoroutine(StartTimer());
    }

    IEnumerator StartTimer()
    {
        yield return new WaitForSeconds(time);
        boxCollider.isTrigger = false;
    }
}
英文:

I refactored your code and fixed the errors
add a tag with the same name (case sensitive)

in the inspector make sure the timer value is > 0

using System.Collections;
using UnityEngine;


public class Walls : MonoBehaviour
{
    [SerializeField] private float time;
    

    // This script is for every wall in the game there is 5 types of walls 

    private BoxCollider boxCollider;

    void Start()
    {
        boxCollider = GetComponent&lt;BoxCollider&gt;();

        Puzzle.OnPuzzleSolved += ActivateWall;                         
    }

    private void OnDisable()
    {
        Puzzle.OnPuzzleSolved -= ActivateWall;
    }
    private void Update()
    {
        
    }

    private void OnTriggerExit(Collider other)
    {
        switch (gameObject.tag)
        { 
            //  if the tag matches We disable the collider on exit so 
            the player can&#39;t go back 
            case &quot;Puzzle Wall&quot;:
                boxCollider.isTrigger = false;
                gameObject.GetComponent&lt;Walls&gt;().enabled = false;
                break;
            case &quot;Fake Wall&quot;:

                boxCollider.isTrigger = true;
                    break;
            case &quot;Puzzle Door&quot;:
                boxCollider.isTrigger = true;
                break;
        }
    }

    private void ActivateWall()
    {
       
       boxCollider.isTrigger = true;
        StartCoroutine(StartTimer());
    }

    IEnumerator StartTimer()
    {

        yield return new WaitForSeconds(time);
        boxCollider.isTrigger = false;
    }
}

huangapple
  • 本文由 发表于 2023年5月24日 17:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定