如何让Invisible Walls脚本工作并生效?

时间:2016-10-06 17:32:00

标签: c# unity3d

这个想法是当玩家走到地形的边缘时,他会停下来,不能继续下降。

在我的情况下,我希望物体在与不可见的墙壁碰撞时向前移动,物体会向后转动并移动到隐形墙壁的另一侧。

我后来读到的另一个问题是,如果物体移动得太快到不可见的墙壁上有一个让它们穿过的虫子?不确定。

这是显示隐形墙的屏幕截图。我创建了一个盒子对撞机,将Is Trigger设置为打开,并将500 600 500设置为与地形大小相同。

Screenshot

这是Invisible Walls的剧本:我把它附加到地形的脚本:

using UnityEngine;
using System.Collections;

public class InvisibleWalls : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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

    }

    void OnTriggerExit(Collider other)
    {

    }
}

这是创建太空船克隆它们并使它们向前移动的脚本。但当他们到达地形的边缘时,他们就出去了。我希望他们能够回到另一边。

此脚本附加到Spheres GameObject:

Screenshot2

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;

public class SphereBuilder : MonoBehaviour
{
    public GameObject SpaceShip;
    GameObject[] spheres;
    public float moveSpeed = 50;
    // for tracking properties change
    private Vector3 _extents;
    private int _sphereCount;
    private float _sphereSize;

    /// <summary>
    ///     How far to place spheres randomly.
    /// </summary>
    public Vector3 Extents;

    /// <summary>
    ///     How many spheres wanted.
    /// </summary>
    public int SphereCount;
    public float SphereSize;

    private void Start()
    {
        spheres = GameObject.FindGameObjectsWithTag("MySphere");
    }

    private void OnValidate()
    {
        // prevent wrong values to be entered
        Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
        SphereCount = Mathf.Max(0, SphereCount);
        SphereSize = Mathf.Max(0.0f, SphereSize);
    }

    private void Reset()
    {
        Extents = new Vector3(250.0f, 20.0f, 250.0f);
        SphereCount = 100;
        SphereSize = 20.0f;
    }

    private void Update()
    {
        UpdateSpheres();
        MoveShips ();
    }

    private void MoveShips()
    {
        foreach (Transform child in spheres[0].transform)
        {
            child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
        }
    }

    private void UpdateSpheres()
    {
        if (Extents == _extents && SphereCount == _sphereCount && Mathf.Approximately(SphereSize, _sphereSize))
            return;

        // cleanup
        var spheres = GameObject.FindGameObjectsWithTag("Sphere");
        foreach (var t in spheres)
        {
            if (Application.isEditor)
            {
                DestroyImmediate(t);
            }
            else
            {
                Destroy(t);
            }
        }

        var withTag = GameObject.FindWithTag("Terrain");
        if (withTag == null)
            throw new InvalidOperationException("Terrain not found");

        for (var i = 0; i < SphereCount; i++)
        {
            var o = Instantiate(SpaceShip);
            o.tag = "Sphere";
            o.transform.SetParent(gameObject.transform);
            o.transform.localScale = new Vector3(SphereSize, SphereSize, SphereSize);

            // get random position
            var x = Random.Range(-Extents.x, Extents.x);
            var y = Extents.y; // sphere altitude relative to terrain below
            var z = Random.Range(-Extents.z, Extents.z);

            // now send a ray down terrain to adjust Y according terrain below
            var height = 10000.0f; // should be higher than highest terrain altitude
            var origin = new Vector3(x, height, z);
            var ray = new Ray(origin, Vector3.down);
            RaycastHit hit;
            var maxDistance = 20000.0f;
            var nameToLayer = LayerMask.NameToLayer("Terrain");
            var layerMask = 1 << nameToLayer;
            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                var distance = hit.distance;
                y = height - distance + y; // adjust
            }
            else
            {
                Debug.LogWarning("Terrain not hit, using default height !");
            }

            // place !
            o.transform.position = new Vector3(x, y, z);
        }

        _extents = Extents;
        _sphereCount = SphereCount;
        _sphereSize = SphereSize;
    }
}

这是一个小型短片,展示太空船到达地形边缘时会发生什么:

Spaceships video clip

更新我到目前为止所做的事情:

在脚本顶部添加:

public Terrain terrain;
private Vector3 boundLower;
private Vector3 boundUpper;

在启动功能中,我添加了:

private void Start()
    {
        spheres = GameObject.FindGameObjectsWithTag("MySphere");

        var withTag = GameObject.FindWithTag("Terrain");
        if (withTag == null)
            throw new InvalidOperationException("Terrain not found");

        boundLower = terrain.transform.position - terrain.transform.size / 2;
        boundUpper = terrain.transform.position + terrain.transform.size / 2;
    }

但两行都有错误:size属性不存在:

boundLower = terrain.transform.position - terrain.transform.size / 2;
boundUpper = terrain.transform.position + terrain.transform.size / 2;

并将MoveShips功能更改为:

private Vector3 direction = Vector3.forward;
    private void MoveShips() {
        foreach (var child in spheres) {
            var pos = child.transform.position + direction * Time.deltaTime * moveSpeed;
            pos.x = Mathf.Clamp(pos.x, boundLower.x, boundUpper.x);
            pos.z = Mathf.Clamp(pos.z, boundLower.z, boundUpper.z);
            if (pos.x == boundLower.x || pos.x == boundUpper.x) direction.x = - direction.x;
            if (pos.z == boundLower.z || pos.z == boundUpper.z) direction.z = - direction.z;
            child.transform.position = pos;
        }
    }

2 个答案:

答案 0 :(得分:0)

我建议修改MoveShips()Vector3.forward更改为变量,并在达到界限时将其翻转:

private Vector3 direction = Vector3.forward;
private void MoveShips() {
    foreach (var child in spheres) {
        var pos = child.transform.position + direction * Time.deltaTime * moveSpeed;
        pos.x = Mathf.Clamp(pos.x, boundLower.x, boundUpper.x);
        pos.z = Mathf.Clamp(pos.z, boundLower.z, boundUpper.z);
        if (pos.x == boundLower.x || pos.x == boundUpper.x) direction.x = - direction.x;
        if (pos.z == boundLower.z || pos.z == boundUpper.z) direction.z = - direction.z;
        child.transform.position = pos;
    }
}

这将消除对对象碰撞引擎的不必要依赖,以实现这么简单的事情。请注意,这是如何使所有在最远到达界限时改变方向。如果您希望它们单独移动,您需要将此逻辑移动到单独的脚本并将其附加到船舶预制件上。

边界(boundLowerboundUpper)可以在编辑器中设置为脚本变量,也可以从地形计算,如:

boundLower = terrain.transform.position - terrain.TerrainData.size / 2;
boundUpper = terrain.transform.position + terrain.TerrainData.size / 2;

我还建议移动它:

var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
    throw new InvalidOperationException("Terrain not found");

Update()进入Start(),除非你在这个过程中做了一些非常时髦的事情。

答案 1 :(得分:-1)

让我们逐一解决你的问题:

问题:对象不会发生碰撞,为什么?

  

答案:物体不会与对撞机碰撞,只能从外部碰撞。

您需要的是4个盒子对撞机,地图的每个边缘都有一个

问题:我后来读到的另一个问题是,如果物体移动得太快到无形的墙壁,那么就会有一个让它们穿过的错误?不确定。

  

这只是物体以类似子弹的速度移动的问题,您可以编辑刚体以具有检测模式:“连续”或连续动态,这将避免此问题

脚本,我认为在这种情况下你不需要它们,你原来的想法很好,只是在整个地形上使用单个对撞机实现,而不是4个单独的“墙”对撞机,是问题。至于其余的逻辑,我没有尝试破译,因此我无法评论它。