- Home /
Mouse Follow (Not Working) - ):
I want a group of Particle to follow the mouse. but can't get it to work and not sure what I'm doing wrong.
here my code
#pragma strict
var mouse : Vector3;
function Start () {
}
function Update ()
{
transform.position = Vector3(Input.mousePosition.x,Input.mousePosition.y,0);
}
PLZ HELP
Answer by Itaros · Oct 25, 2014 at 04:41 PM
It is absolutely correct if your camera is orthographic and pixel-perfect, otherwise to make it compliant with current camera VP setting use this: http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
If there is a need to use that with perspective camera I recommend to set plane(or better - quad) with collider and cast ray to that plane. That way you will have full control on movement plane without the need to mess with projection math and overcomplicating code. The good helper in that is Physics.Raycast: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)){
transform.position = hit.point;
}
Take note regarding two things: If y looks reversed in comparison to screen just perform flipping of it by Screen.height. You may need to add mask to make sure in this pass only plane is detected instead of other colliders. Experiment ;)
Yeah I Did try Camera.ScreenToWorldPoint, but I just can't get it to work with the script I'm not sure how to write it.
Plz Help.
Then describe the problem with more detail if possible. I don't get it.
Ok so I change the script a little
mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = mouse;
and set the camera to orthographic, and it work Great, but now I need to get it to work with Perspective.
Let me update my answer to accommodate this requirement :)
Your answer
Follow this Question
Related Questions
Mouse follow on network 0 Answers
2D game with 360 degree aiming and shooting a particle system... help please 1 Answer
Reacting to Mouse animation... 2 Answers
swiping with mouse input 0 Answers