- Home /
Move object towards mouse but if mouse moves object fallows it's original path
I want to move an object from it's original position to where the mouse is clicked but if the mouse moves, the object should continue going in it's original path. I only use c#! Any Suggestions?
You could always use a Ray co$$anonymous$$g from the main camera, then use a RaycastHit to deter$$anonymous$$e the new position you want the object to travel to. You would attach a script like this to the object you want to move. This requires that you have some sort of terrain or other collider that you can click on for your object to travel to.
//Example.cs
//C-Sharp
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
Vector3 newPosition;
Ray ray;
RaycastHit hit;
void Awake()
{
newPosition = transform.position;
}
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit) && Input.Get$$anonymous$$ouseButtonDown (0))
{
newPosition = hit.point;
}
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, newPosition, 5.0f * Time.smoothDeltaTime);
}
}
It worked partially. I can only click on an object I want to be able to click anywhere on the screen. Also it was not specified in the question, but the object is a prefab witch will be destroyed when it goes off the screen. Thanks.