我正在使用脚本在随机位置围绕地形创建游戏对象,但在同一个地方不应该有游戏对象。
当我克隆ThirdPersonController它的大小时的第一个问题与我克隆原始的那个不一样。
原始比例是1,1,1 但是当我克隆其他人时,比例是:1,14,1而其他人是1,17,1他们都不像原版。原来是唯一仍然没法的人。 这是游戏运行时“场景”窗口的屏幕截图。在右侧的检查器中,您可以看到缩放Y的值为22。
如果我将值更改为1,它会没问题,但为什么它们都是用不同的Y值创建的?如果我创建其他GameObjects也会出现同样的问题。
这是GameObject的屏幕截图,我将脚本附加到:
这是游戏运行时的屏幕截图以及克隆对象的外观。在这种情况下,该克隆的Y值为28:
脚本:
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CloneObjects : MonoBehaviour
{
public GameObject ObjectToCreate;
public int objectsHeight = 3;
[HideInInspector]
public GameObject[] objects;
// for tracking properties change
private Vector3 _extents;
private int _objectCount;
private float _objectSize;
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int ObjectCount;
public float ObjectSize;
// Use this for initialization
void Awake()
{
Clone();
objects = GameObject.FindGameObjectsWithTag("ClonedObject");
}
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));
ObjectCount = Mathf.Max(0, ObjectCount);
ObjectSize = Mathf.Max(0.0f, ObjectSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
ObjectCount = 100;
ObjectSize = 20.0f;
}
// Update is called once per frame
void Update()
{
}
private void Clone()
{
if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
return;
// cleanup
var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
foreach (var t in ObjectsToDestroy)
{
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 < ObjectCount; i++)
{
var o = Instantiate(ObjectToCreate);
o.tag = "ClonedObject";
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(ObjectSize, Random.Range(3, 50)/*ObjectSize*/, ObjectSize);
// 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 + objectsHeight, z);
}
_extents = Extents;
_objectCount = ObjectCount;
_objectSize = ObjectSize;
}
}
我想要做的是能够克隆任何游戏对象,并且克隆应该与原始相同的大小相同。
另一个问题是我想收集GameObject下的所有角色,所以我添加了一个名为它的空游戏对象:角色。 当我将ThirdPersonController拖动为Character的子元素时,克隆的对象离开地形并掉落。
最后,我仍然得到一个警告,不确定这是否会导致问题,但我无法弄清楚为什么我会收到警告以及如何解决/修复它:
地形未击中,使用默认高度! UnityEngine.Debug:LogWarning(对象) CS:109) CS:34)
第109行是:
Debug.LogWarning("Terrain not hit, using default height !");
第34行:
Clone();