- Home /
Move to mouse position
So I've searched around enough, unable to find a good solution to my problem... I'm trying to make an object (or maybe multiple objects at once) Lerp towards a point. I'm trying to make that point be defined by a raycast, but I've had trouble with both making the raycast hit point be registered as Vector3, and once I finally got it to stop making that error message now my objects simply won't move. I'm suspecting that the raycast isn't actually registering as the end point for my Lerp.
here's a snippet of my code that i'm trying to make work.
var speed:float;
var target : Vector3;
var start : Vector3;
function Start()
{
start = transform.position;
}
function Update ()
{
if(Input.GetButtonDown("2"))
{
var target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = Vector3.Lerp(start, target, speed);
}
}
Still quite new to programming, so please explain anything even very simple with detail :)
Edit: solved, final code now is as follows :D
var speed:float;
var target : Vector3;
var start : Vector3;
private var pos;
function Start()
{
start = transform.position;
pos = transform.position;
}
function Update ()
{
if(Input.GetButton("2"))
{
pos = Input.mousePosition;
pos.z = 45;
pos = Camera.main.ScreenToWorldPoint(pos);
}
transform.position = Vector3.Lerp(transform.position, pos, speed*Time.deltaTime);
}
This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here
$$anonymous$$ove to Touch / Click Position - The Game Contriver
Answer by robertbu · Mar 29, 2013 at 09:45 PM
There are a couple of problems here. First ScreenToWorldPoint() uses the 'z' of the parameter to say how far into the scene to place an object. Unity is a 3D environment, so it's world position will depend on the distance from the camera. The Input.mousePosition's 'Z' value is 0. So you can need to do something like:
var pos = Input.mousePosition;
pos.z = 10;
pos = Camera.main.ScreenToWorldPoint(pos);
The next issues is that your position assigning should not be inside the if() statement, and needs to be reworked. Try this:
transform.position = Vector3.Lerp(transform.position, pos, speed * Time.deltaTime);
Note this is a "misuse" of Lerp() in some ways, but it produces a easing at the end of the move. You could change the Lerp() to a Vector3.MoveTowards() for an uneased movement.
Trying your method, I was successful in making them move.. but they wont seem to just go to the mouse. When i press my button that I want to set their endpoint (2), they will twitch that way for a moment, but immediately afterwards they just start going to 0,0,0 again (that's just the default end point they have i guess). I can get the desired effect if i change the script to GetButton("2") but I dont want the end point to constantly change. Do I have to put the var pos, pos.z, and pos part that you suggested under the function Update?
Post your code. I'm sure I can spot the problem quickly.
the only change I made so far is
var speed:float;
var target : Vector3;
var start : Vector3;
function Start()
{
start = transform.position;
}
function Update ()
{
if(Input.GetButton("2"))
{
var pos = Input.mousePosition;
pos.z = 45;
pos = Camera.main.ScreenToWorldPoint(pos);
}
transform.position = Vector3.Lerp(transform.position, pos, speed*Time.deltaTime);
}
Here is a few changes. The primary problems was that pos was declared locally to Update() rather than at the top of the file. $$anonymous$$y fault given the code I provided you. I set speed to 10. If you already have the script attached, you'll have to make the change in the inspector. I am also using Get$$anonymous$$ouseButton(), so you can change that back when you've verified the code:
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.Get$$anonymous$$ouseButton(0))
{
pos = Input.mousePosition;
pos.z = 45;
pos = Camera.main.ScreenToWorldPoint(pos);
}
transform.position = Vector3.Lerp(transform.position, pos, speed*Time.deltaTime);
}
I got stuck for the longest time here because I didn't notice you removed "var" from in front of the "pos" after you changed it around :P Works just how I wanted now, thank you!
Answer by MIkeross3 · Jun 06, 2015 at 09:24 PM
I can never find the right answer online, and I always forget lol. But here is the answer for all people searching like me!
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
target = new Vector3 (hit.point.x, 0.01f, hit.point.z);
}
if (Input.GetKeyDown ("w")) {
Instantiate (roadPrefab, target, Quaternion.identity);
}
Road = GameObject.FindGameObjectWithTag ("Road");
if(Road != null)
{
Road.transform.position = target;
}
}
0.01f can be changed to anything. That is just a lock on the Y axis I wanted. The whole issue is the if statement locking out the mouse position for most cases.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
constantly updates the mouse position to a Ray. If your mouse is over another object, hit will be true. Then if updates the target position. If the road is there, move it to the mouse position.
I know this doesn't directly answer your question, but this is a way to instantiate an object and keep it over mouse position. It needs tuning obviously, but you get the jist. Hope this helps someone!