- Home /
OnMouseUp() Click Display Effect.
Hi,
I was wondering how would I go about displaying an object, or a particle effect every time I OnMouseUp() click on the terrain. Similar to a RTS game. When you click on the destination, a particle or object is displayed showing where your destination is. Thank you very much.
Answer by markpdolby · Jun 28, 2013 at 09:15 AM
I would suggest using a ray cast from the mouse position onto the terrain:
[SerializeField] private ParticleSystem effect;
void Update(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100, LayerMask.NameToLayer("Terrain")))
{
effect.transform.postion = hit.point;
effect.Play();
}
}
Thank you for your response. I am still getting used to coding. Should I declare a variable for it? Where should I add the particle? I can not seem to get it to work in Javascript or C#. I am really sorry if this question has an obvious answer.
Sorry it was a bit pseudo code, I've edited my answer so see if that works now. So in your scene attach the script to the main camera for example and create a particle system as a seperate object. On the script then drag the particle system object onto the effect slot in the inspector. This is assu$$anonymous$$g you are using Unity 4.
That is okay, I really appreciate your help. I am getting this error now.
Assets/ParticleSystem.cs(16,20): error CS1061: Type ParticleSystem' does not contain a definition for
Play' and no extension method Play' of type
ParticleSystem' could be found (are you missing a using directive or an assembly reference?)
You need to name your script with a different name as Unity already uses the name ParticleSystem. So rename your script to something like $$anonymous$$ouseControls
I think it may be me. I can not get it to work for some reason. I added the particle as a GameObject and attached it to the script that is attached to the camera. I click and it does not appear where I click, or at all (only the original one shows). Your help is very clear, I must be doing something wrong.
Your answer
Follow this Question
Related Questions
Dragging movement Speed 1 Answer
Object Movement via Mouse Click? 3 Answers
Click and Drag Camera 3 Answers
Object menu must be at the center of the object 1 Answer
How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers