- Home /
put object on mouse position
Need some help in a question. A need to make a object appear at the some position at the mouse position on mouse click. With my script i can only get the object start at position x and when i click it goes to mouse position. How can i make it start at mouse position??
this is my script:
var speed:float = 10.0; var target : Vector3; var start : Vector3; private var pos;
function Start() { start = transform.position; pos = transform.position; }
function Update () { if(Input.GetMouseButton(0)) {
pos = Input.mousePosition;
pos.z = 45;
pos = Camera.main.ScreenToWorldPoint(pos);
}
transform.position = Vector3.Lerp(transform.position, pos, speed*Time.deltaTime);
}
Someone help??
Answer by robertbu · Nov 13, 2013 at 12:14 AM
Line 5 should be:
transform.position = pos;
This will cause the object with this script to instantly appear at the mouse position.
It didn´t work. The result is the same. $$anonymous$$aybe ray cast??
You would want to add a Debug.Log, after the pos = Camera.main.ScreenToWorldPoint(pos); line, because if you dont get the mousePosition or the Camera is erroring, your ScreeToWorldPoint will not work.
Here is the code stripped down to only what is needed for an instant move:
#pragma strict
function Update () {
if(Input.Get$$anonymous$$ouseButton(0)) {
var pos = Input.mousePosition;
pos.z = 45;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position = pos;
}
}
Why a Debug.Log ??'. I did it but it didn´t change. $$anonymous$$Aybe a example?? or i´m not follow...
Excellent. It worked!!! The thing was to remove the start conditions. Thak you. help me a lot.
Your answer
Follow this Question
Related Questions
How to detect mouse movement as an input 4 Answers
mouse position on tag 1 Answer
Mobile controls, gui mouse problem. 0 Answers
Scale i rotate by mouse position 0 Answers
Unity 2d object advanced rotation 1 Answer