【发布时间】:2020-06-22 17:53:28
【问题描述】:
首先我想说的是我……对此非常陌生。希望这不是一个愚蠢的问题。我刚刚完成了一个脚本,它允许我砍倒一棵树并在树消失后生成 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);
}
}
【问题讨论】:
-
当你调用“Instantiate(treeLogTwo, transform.position, transform.rotation);”时transform.position 是生成片段的位置,将其更改为所需位置
-
我明白了!现在是相对于树的位置吗?或者如果我改变它,它是否会基于地图上的其他地方?
-
取决于你的目标是位置还是localPosition
标签: c# unity3d game-engine