- Home /
 
Want to move object slowly to where the mouse clicks?
I can move it instantly so far, and I also need to maintain level height (y=40)
 if (Input.GetMouseButtonDown(0))
 {
 Plane plane = new Plane(Vector3.up, new Vector3(0, 40, 0));
 Ray ray = 
 Camera.main.ScreenPointToRay(Input.mousePosition);
 if (plane.Raycast(ray, out float distance))
 {
 transform.position = ray.GetPoint(distance);
 }
 }
 
              
               Comment
              
 
               
              Answer by Zaeran · Sep 08, 2020 at 04:32 PM
When you click, you want to store the position of the click in a variable. Let's just call this
Vector3 clickPos.
So now:
  if (plane.Raycast(ray, out float distance))
  {
  clickPos = ray.GetPoint(distance);
  }
 
               Now that we have our click position stored, we can move towards it in our Update loop.
 void Update ()
 {
     transform. position = Vector3.MoveTowards(transform.position, new Vector3(clickPos.x, 40, clickPos.z), moveDistance);
 }
 
              Your answer
 
             Follow this Question
Related Questions
Raycasting on mask doesnt work 2 Answers
Dashing through enemies, but not walls. 1 Answer
Locking gameobject to rotating floor 0 Answers
Player moves too fast and won't move to point 1 Answer
Drawing flat arrows 1 Answer