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??
It didn´t work. The result is the same. Maybe ray cast??
– joanYou would want to add a Debug.Log, after the
– Starwalkerpos = 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.GetMouseButton(0)) { var pos = Input.mousePosition; pos.z = 45; pos = Camera.main.ScreenToWorldPoint(pos); transform.position = pos; } }
– robertbuWhy a Debug.Log ??'. I did it but it didn´t change. MAybe a example?? or i´m not follow...
– joanExcellent. It worked!!! The thing was to remove the start conditions. Thak you. help me a lot.
– joan