[UNITY][Particle] Particle ที่เล่น Particle เดิมซ้ำๆ แต่ Random จุดเกิด by

30
Jun
0

เคยไหม ที่อยากให้ Particle เกิดแบบ Random จุดเกิด แต่มีปริมาณตามที่เรากำหนดไว้ตลอด ยกตัวอย่างเช่น ทำ Particle สำหรับ ท่าโจมตีของ Unit ตัวหนึ่งซึ่งเป็นมือปืน เวลาใช้ท่าไม้ตาย Unit ตัวนี้จะโจมตี 6 ครั้งแบบยิงกราด อะไรประมาณนั้น วันนี้จะมานำเสนอ Code ที่จะทำให้ชีวิตง่ายขึ้นครับ

ขั้นแรก เอา Code ไปก่อนเลยครับ
using UnityEngine;
using System;
using System.Collections;

public class ParticleRandomize : MonoBehaviour
{
[Serializable]
public class Range
{
public float Min;
public float Max;
}

public GameObject Particle;
public int Count;
public float Time;
public Range X;
public Range Y;
public float RandomDistancePercent = 100f;

void Start()
{
float TimePerOnce = Time / Count;
Vector2 Distance = new Vector2(Mathf.Abs(X.Min – X.Max) * RandomDistancePercent / 100 / 2, Mathf.Abs(Y.Min – Y.Max) * RandomDistancePercent / 100 / 2);
Vector2 lastPosition = Vector3.zero;

for (int i = 0; i 100)
{
break;
}
} while ((lastPosition.x + Distance.x > x && x > lastPosition.x – Distance.x) && (lastPosition.y + Distance.y > y && y > lastPosition.y – Distance.y));
lastPosition = new Vector2(x, y);
_particle.transform.parent = transform;
_particle.transform.localPosition = new Vector2(x, y);
});
}
}
}

ต่อมาเราก็ AddComponent ให้ Particle ที่เราอยากให้มันเล่นซ้ำๆนะครับ ตัวอย่าง

Enjoy this article?

Consider subscribing to our RSS feed!

Tagged as: ,

No Comments

No comments yet.

Leave a comment

RSS feed for comments on this post

 เราชนะรอบ 4 | ยืมเงิน 3000 ด่วน | แอพกู้เงิน | แอพเงินด่วน | สินเชื่อออนไลน์อนุมัติทันที | Site Map | กู้เงินก้อน | กระเป๋าตัง | thisshop และ ยืมเงินฉุกเฉิน 5000 ด่วน