Instantiate a particle system, probably a stupid question
hey guys, new to unity and programming.
the function I want to realize is like:
click A(startpoint) , right click B(endpoint), instantiate a particle system from A to B.
its not finish, cause I know there are many logic problems here, can you guys give me some hint on how i can make this work? also,why i can't use startpoint to assign a value to flowClone.transform.position?
Thanks!
void connection()
{
Vector3 startpoint;
Vector3 endpoint;
ParticleSystem flowClone;
bool startpointget;
bool endpointget;
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
startpoint = hit.collider.transform.position;
}
}
if (Input.GetMouseButton(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
endpoint = hit.collider.transform.position;
}
}
flowClone = Instantiate(flow, Vector3.zero, Quaternion.identity) as ParticleSystem;
flowClone.transform.position = startpoint;
}
If you split off some of your questions I might be able to help you out a little better
Answer by Gilles_aerts · Oct 06, 2015 at 05:51 PM
Hi , I'm not sure what your real question is , but you cannot instantiate a particle system variable ( atlas not to my knowledge) . if you want to instantiate a particleSystem you have to make a gameObject of it. so instead of initializing : particleSystem FlowClone ; make a gameObject : Public Gameobject FlowClone;
assign the particle systemPrefab to the public gameObject variable in your inspector. I was always learned that you can only instantiate gameObjects so this might do the trick.
sorry for my uncleard question.
$$anonymous$$y question can be split into several part:
1: i want instantiate a particleSystem on a position of other objects. So I use Ray to select objects, and get the position from that.(as you can see in line15). But it seems i can't assign the startpoint to flowclone.tranform.position. theres a red line under the "startpoint" in line30.
2:Is there a better logic to do this: instantiate a particle system from startpoint to endpoint.
hope i make it clear. thanks for replying.
alright here is what you need to do. You need to make a prefab of the particleSystem you wish to instantiate. Then you need to make a public GameObject variable in your script and name it something like myPartcleSystem or flowClone as you did earlier. so you should have ( public GameObject flowClone; )
the instantiate functions ask for 3 things( what prefab , what position, and what rotation) But looking at your code you already knew that ;)
now if you want to instantiate it on the same position as an object you just simply use his position : instantiate(flowCone,objectIwant.transform.posistion,quaternion.identity); the objectIwant is most likely another public game object in the scene. you can also use a transform variable but then you have to leave out the transform.position.
Answer by Sernostine · Oct 09, 2015 at 02:22 PM
Thanks, guy!
it works. Assigning a initial value at the first is also necessary.