Move to mouse position (67238)

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 :slight_smile:

Edit: solved, final code now is as follows :smiley:

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);
}

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.

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!