C# 在特定位置实例化对象?

C# 在特定位置实例化对象?,c#,unity3d,game-engine,C#,Unity3d,Game Engine,我想先说我。。。这是很新的。希望这不是一个愚蠢的问题。我刚刚完成了一个脚本,它允许我砍倒一棵树,并在树消失后生成3块木头。我遇到的问题是,一旦原木产卵,它们就会站起来产卵,并在树的同一位置相互堆叠 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class TreeAi : MonoBehaviour { GameObject t

我想先说我。。。这是很新的。希望这不是一个愚蠢的问题。我刚刚完成了一个脚本,它允许我砍倒一棵树,并在树消失后生成3块木头。我遇到的问题是,一旦原木产卵,它们就会站起来产卵,并在树的同一位置相互堆叠

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class TreeAi : MonoBehaviour
{
GameObject thisTree;
public int treeHealth = 35;
private bool isFallen = false;
public GameObject treeLog;
public GameObject treeLogOne;
public GameObject treeLogTwo;
public AudioClip treeFall;
private void Start()
{
thisTree = transform.gameObject;
}
void DeductPoints(int damageAmount)
{
treeHealth -= damageAmount;
}
void Update()
{
if (treeHealth <= 0 && isFallen == false)
{
Rigidbody rb = thisTree.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.useGravity = true;
rb.AddForce(Vector3.forward, ForceMode.Impulse);
StartCoroutine(destroyTree());
isFallen = true;
AudioSource.PlayClipAtPoint(treeFall, this.gameObject.transform.position);
}
}
private IEnumerator destroyTree()
{
yield return new WaitForSeconds(2.2f);
Destroy(thisTree);
Instantiate(treeLog, transform.position, transform.rotation);
Instantiate(treeLogOne, transform.position, transform.rotation);
Instantiate(treeLogTwo, transform.position, transform.rotation);
}
}
有没有办法让它们稍微散开产卵,然后躺在树倒下的地方

这是我的树脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class TreeAi : MonoBehaviour
{
GameObject thisTree;
public int treeHealth = 35;
private bool isFallen = false;
public GameObject treeLog;
public GameObject treeLogOne;
public GameObject treeLogTwo;
public AudioClip treeFall;
private void Start()
{
thisTree = transform.gameObject;
}
void DeductPoints(int damageAmount)
{
treeHealth -= damageAmount;
}
void Update()
{
if (treeHealth <= 0 && isFallen == false)
{
Rigidbody rb = thisTree.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.useGravity = true;
rb.AddForce(Vector3.forward, ForceMode.Impulse);
StartCoroutine(destroyTree());
isFallen = true;
AudioSource.PlayClipAtPoint(treeFall, this.gameObject.transform.position);
}
}
private IEnumerator destroyTree()
{
yield return new WaitForSeconds(2.2f);
Destroy(thisTree);
Instantiate(treeLog, transform.position, transform.rotation);
Instantiate(treeLogOne, transform.position, transform.rotation);
Instantiate(treeLogTwo, transform.position, transform.rotation);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.AI;
公共类树:单一行为
{
游戏对象树;
公共int treeHealth=35;
private bool isfall=false;
公共游戏对象树;
公共游戏对象树;
公共游戏对象树2;
公共音频剪辑treeFall;
私有void Start()
{
ThistTree=transform.gameObject;
}
空扣点数(内部损坏金额)
{
树木健康-=损害金额;
}
无效更新()
{

if(treeHealth进入
Instantiate()
的第二个参数是您希望放置对象的位置。在这种情况下,您使用的位置与树相同

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class TreeAi : MonoBehaviour
{
GameObject thisTree;
public int treeHealth = 35;
private bool isFallen = false;
public GameObject treeLog;
public GameObject treeLogOne;
public GameObject treeLogTwo;
public AudioClip treeFall;
private void Start()
{
thisTree = transform.gameObject;
}
void DeductPoints(int damageAmount)
{
treeHealth -= damageAmount;
}
void Update()
{
if (treeHealth <= 0 && isFallen == false)
{
Rigidbody rb = thisTree.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.useGravity = true;
rb.AddForce(Vector3.forward, ForceMode.Impulse);
StartCoroutine(destroyTree());
isFallen = true;
AudioSource.PlayClipAtPoint(treeFall, this.gameObject.transform.position);
}
}
private IEnumerator destroyTree()
{
yield return new WaitForSeconds(2.2f);
Destroy(thisTree);
Instantiate(treeLog, transform.position, transform.rotation);
Instantiate(treeLogOne, transform.position, transform.rotation);
Instantiate(treeLogTwo, transform.position, transform.rotation);
}
}
您需要稍微修改位置向量。Unity有一个
Random
方法,在这种情况下非常有用

您可以像这样更新代码以使用此偏移量

// How far the random offset can stretch var randomSpawnRadius = 2.0f; // Take the current position and add a random offset times the radius var treeLogOffset = transform.position += Random.insideUnitSphere * randomSpawnRadius; Instantiate(treeLog, treeLogOffset , transform.rotation); // Get another random position inside the radius treeLogOffset = transform.position += Random.insideUnitSphere * randomSpawnRadius; Instantiate(treeLogOne, treeLogOffset , transform.rotation); treeLogOffset = transform.position += Random.insideUnitSphere * randomSpawnRadius; Instantiate(treeLogTwo, treeLogOffset , transform.rotation); } //随机偏移可以延伸多远 var半径=2.0f; //取当前位置并添加一个随机偏移乘以半径 var treeLogOffset=transform.position+=Random.insideuUnitSphere*randomSpawnRadius; 实例化(treeLog、treeLogOffset、transform.rotation); //在半径内获得另一个随机位置 treeLogOffset=transform.position+=Random.insideUnitSphere*Random半径; 实例化(treeLogOne、TreeLoGoOffset、transform.rotation); treeLogOffset=transform.position+=Random.insideUnitSphere*Random半径; 实例化(treeLogTwo、treeLogOffset、transform.rotation); } 另一种方法是将其作为一个函数,因为它是相当重复的

private void InstantiateAtRandomOffset(GameObject objectToInstantiate, Transform transform, float randomSpawnRadius) { var itemOffset= transform.position += Random.insideUnitSphere * randomSpawnRadius; Instantiate(objectToInstantiate, itemOffset, transform.rotation); } 私有void实例化随机偏移(GameObject ObjectToInstance、Transform Transform、float randomSpawnRadius) { var itemOffset=transform.position+=Random.InsideuUnitSphere*randomRadius; 实例化(ObjectToInstate、itemOffset、transform.rotation); } 然后,您可以将代码中的调用替换为

InstantiateAtRandomOffset(treeLog, transform, 2.0f); InstantiateAtRandomOffset(treeLogOne, transform, 2.0f); InstantiateAtRandomOffset(treeLogTwo, transform, 2.0f); 实例化随机偏移(树状图、变换、2.0f); 实例化随机偏移(treeLogOne,变换,2.0f); 实例化随机偏移(树高2,变换,2.0f);
当您调用“实例化(treeLogTwo,transform.position,transform.rotation);”transform.position是生成工件的位置,将其更改为所需的位置我知道了!现在是相对于树的位置吗?或者如果我更改了位置,它是否基于地图上的其他位置?取决于您是针对该位置还是localPosition