【问题标题】:When I used portal in My Game why Destroyed "Camera Fallow"?当我在我的游戏中使用传送门时,为什么要销毁“Camera Fallow”?
【发布时间】:2020-01-12 07:32:02
【问题描述】:

我制作了一个通往 2D 游戏的门户。通常,相机需要跟随角色。但是在我编写的门户脚本之后,“CameraFallowScript”不起作用。角色正在穿过传送门。但经过“CameraFallowScript”后消失。我有点新,我的英语很差。 感谢您的帮助。

Camera Fallow Script :

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

public class CameraFallow : MonoBehaviour
{
    public GameObject target;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        transform.position = new Vector3(target.transform.position.x, target.transform.position.y, transform.position.z);


    }
}

这里的门户脚本:

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

public class Portal : MonoBehaviour
{
    private Rigidbody2D enteredRigidbody;
    private float enterVelocity, exitVelocity;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        enteredRigidbody = collision.gameObject.GetComponent<Rigidbody2D>();
        enterVelocity = enteredRigidbody.velocity.x;

        if (gameObject.name == "BluePortal")
        {
            PortalControl.portalControlInstance.DisableCollider("orange");
            PortalControl.portalControlInstance.CreateClone("atOrange");
        }
        else if (gameObject.name == "OrangePortal")
        {
            {
                PortalControl.portalControlInstance.DisableCollider("blue");
                PortalControl.portalControlInstance.CreateClone("atBlue");
            }
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        exitVelocity = enteredRigidbody.velocity.x;

        if (enterVelocity != exitVelocity)
        {
            Destroy(GameObject.Find("Clone"));
        }
        Destroy(collision.gameObject);
        PortalControl.portalControlInstance.EnableColliders();
        GameObject.Find("Clone").name = "Character";
        CameraFallow.DontDestroyOnLoad(transform.gameObject);




    }
}

此处为 PortalControl 脚本:

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

public class PortalControl : MonoBehaviour
{
    public static PortalControl portalControlInstance;

    [SerializeField]
    private GameObject bluePortal, orangePortal;

    [SerializeField]
    private Transform bluePortalSpawnPoint, orangePortalSpawnPoint;
    private Collider2D bluePortalCollider, orangePortalCollider;

    [SerializeField]
    private GameObject clone;
    void Start()
    {
        portalControlInstance = this;
        bluePortalCollider = bluePortal.GetComponent<Collider2D>();
        orangePortalCollider = orangePortal.GetComponent<Collider2D>();
    }

    // Update is called once per frame
    public void CreateClone(string whereToCreate)
    {
        if (whereToCreate == "atBlue")
        {
            var instantiatedClone = Instantiate(clone, bluePortalSpawnPoint.position, Quaternion.identity);
            instantiatedClone.gameObject.name = "clone";
        }
        if (whereToCreate == "atOrange")
        {
            var instantiatedClone = Instantiate(clone, orangePortalSpawnPoint.position, Quaternion.identity);
            instantiatedClone.gameObject.name = "clone";
        }
    }
    public void DisableCollider(string ColliderToDisable)
    {

        if (ColliderToDisable == "orange")
        {
            orangePortalCollider.enabled = false;

        }
        else if (ColliderToDisable == "blue")
        {
            bluePortalCollider.enabled = false;
        }
    }
public void EnableColliders()
    {
        orangePortalCollider.enabled = true;
        bluePortalCollider.enabled = true;
    }
}

【问题讨论】:

    标签: c# visual-studio unity3d game-engine


    【解决方案1】:

    我猜我一般不会创建克隆...为什么要实例化一个新播放器?为什么不简单地将其移动到新位置?

    这里发生的情况是,在 Destroy(collision.gameobject) 之后,FollowCamera 失去了对其 target 的引用,因此您需要在克隆后重新分配它,例如在

    private void OnTriggerExit2D(Collider2D collision)
    {
        exitVelocity = enteredRigidbody.velocity.x;
    
        if (enterVelocity != exitVelocity)
        {
            Destroy(GameObject.Find("Clone"));
        }
        Destroy(collision.gameObject);
        PortalControl.portalControlInstance.EnableColliders();
        var clone = GameObject.Find("Clone");
        clone.name = "Character";
        DontDestroyOnLoad(clone);
    
    
        // It would ofcourse be way more efficient 
        // if you would store this reference somewhere
        FindObjectOfType<CameraFollow>().target = clone;
    }
    

    请注意,Find 的一般用法很昂贵,您应该尽可能避免使用它!而是在所有必需的脚本之间传递 clone 引用。


    关于编码风格:传递string 参数并不是真正好的代码。

    我建议例如enum 喜欢

    public enum PortalSide
    {
        orange,
        blue
    }
    

    然后使用

    private Dictionary<PortalSide, Transform> portalSpawns;
    private Dictionary<PortalSide, Collider> portalColliders;
    
    private void Awake()
    {
        portalSpawns = new Dictionary<PortalSide, Transform> { {PortalSide.blue, bluePortalSpawnPoint} , {PortalSide.orange, orangePortalSpawnPoint}};
        portalColliders = new Dictionary<PortalSide, Collider> { {PortalSide.blue, bluePortalCollider}, {PortalSide.orange, orangePortalCollider} };
    }
    
    public void CreateClone(PortalSide whereToCreate)
    {
        var spawnPoint = PortalSides[whereToCreate];
    
        var instantiatedClone = Instantiate(clone, spawnPoint.position, Quaternion.identity);
        instantiatedClone.gameObject.name = "clone";
    }
    
    public void DisableCollider(PortalSide ColliderToDisable)
    {
        var colliderToDisable = portalColliders[ColliderToDisable];
    
        colliderToDisable.enabled = false;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多