Edit: New answer based on new informaton
I will keep the old answer since the code is not functional without it, if "move towards mouse position" would be the desired behaviour, as the title suggests.
Problem
You're trying to get the object to do the same movements as the mouse is doing, but the code you've written takes in the position of the mouse, not the movement of the mouse, and moves towards it.
Solution
You must check the axis of the mouse, and apply that movement direction to the object, something like:
float speed = 5f;
void Update()
{
if (Input.GetMouseButton(0))
{
var moveDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
transform.position += moveDirection * speed * Time.deltaTime;
}
}
Old answer
Problem
I'm guessing your Lerp isn't doing what you want it to do, since the code will only execute once, and is setting your position to Time.deltaTime*5, which should roughly be 0.9, which means 90% of the distance from transform.position
to hitPoint
. For more information about Lerping, please read the documentation:
Vector3 Lerp(Vector3 a, Vector3 b, float t);
- When t = 0: returns a.
- When t = 1 returns b.
- When t = 0.5 returns the point midway between a and b.
Solution
I think a simple Vector3.MoveTowards will work for you:
Vector3 hitPoint;
float speed = 5;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float enter = 0.0f;
if (m_Plane.Raycast(ray, out enter))
{
hitPoint = ray.GetPoint(enter);
transform.position = Vector3.MoveTowards(transform.position, hitPoint, speed * Time.deltaTime);
}
}
}