粒子在错误的地方产生
我正在 Unity 中制作一款游戏,你可以在其中砍倒树木,我想让粒子在你击中树木的地方产生。此时粒子会在玩家所在的位置生成,这是因为脚本位于玩家身上。但如何才能在正确的位置生成粒子呢? (我撞到树的地方)这可能并不难解决,但我无法弄清楚。我当前的 C# 代码如下。
public class ChopTree : MonoBehaviour
{
public int damage = 25;
public Camera FPSCamera;
public float hitRange = 2.5f;
private TreeScript Tree;
// Particles
public GameObject particles;
void Update()
{
Ray ray = FPSCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if(Input.GetKeyDown(KeyCode.Mouse0))
{
if(Physics.Raycast(ray, out hitInfo, hitRange))
{
// The tag must be set on an object like a tree
if(hitInfo.collider.tag == "Tree" && isEquipped == true)
{
Tree = hitInfo.collider.GetComponentInParent<TreeScript>();
StartCoroutine(DamageTree());
StartCoroutine(ParticleShow());
}
}
}
}
private IEnumerator DamageTree()
{
// After 0.3 seconds the tree will lose HP
yield return new WaitForSeconds(0.3f);
Tree.health -= damage;
}
private IEnumerator ParticleShow()
{
// After 0.3 second the particles show up
yield return new WaitForSeconds(0.3f);
Instantiate(particles, transform.position, transform.rotation);
}
}
I'm making a game in Unity where you can chop down trees, I want to make it so that particles spawn where you hit the tree. At this point the particles spawn where the player is, this is because the script is on the player. But how can I spawn the particles in the right place? (Where I hit the tree) It's probably not even that hard to solve, but I can't figure it out. My current C# code is below.
public class ChopTree : MonoBehaviour
{
public int damage = 25;
public Camera FPSCamera;
public float hitRange = 2.5f;
private TreeScript Tree;
// Particles
public GameObject particles;
void Update()
{
Ray ray = FPSCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if(Input.GetKeyDown(KeyCode.Mouse0))
{
if(Physics.Raycast(ray, out hitInfo, hitRange))
{
// The tag must be set on an object like a tree
if(hitInfo.collider.tag == "Tree" && isEquipped == true)
{
Tree = hitInfo.collider.GetComponentInParent<TreeScript>();
StartCoroutine(DamageTree());
StartCoroutine(ParticleShow());
}
}
}
}
private IEnumerator DamageTree()
{
// After 0.3 seconds the tree will lose HP
yield return new WaitForSeconds(0.3f);
Tree.health -= damage;
}
private IEnumerator ParticleShow()
{
// After 0.3 second the particles show up
yield return new WaitForSeconds(0.3f);
Instantiate(particles, transform.position, transform.rotation);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,
我不会像实际上个人那样确保使用命中树位置,
而是将两个协程合并在一起并传入相应的树:
然后
Well instead of
make sure you use the hit tree positions like
Actually personally I would merge both Coroutines together and pass in the according tree:
and then
如果您通过单击屏幕来砍树,那么您可以在“击中”对象所在的位置生成它,但是,如果您尝试实例化粒子来代替击中对象,它将是树的原点,因此,您可以将树的碰撞器添加到树的表面并使其成为不同的对象(也可以将其设为子对象)。所以这种方式不是很光滑,但是你可以通过这种方式在树的表面创建粒子。
另外,如果你用角色来砍它,你可以添加启用 onTrigger 的对撞机到树上,当你触发时,你会在触发对象所在的位置生成一个粒子。
If you are chopping the tree by clicking to screen then, you could spawn it where "hit" object is but, if you try to instantiate particle in place of hit object it would be a tree's origin so, you could add tree's collider to the surface of the tree and make it different object (Also you can make it child). So this way is not very smooth but you could create particles on the surface of the tree by this way.
Also if you are chopping it with character then, you could add collider which have enable onTrigger to tree and when you triggered you spawn a particle at where triggered object is.