Object the mouse

When my mouse to click the object, and then move my mouse at random.

When the mouse stops moving, objects, how to follow this path?

Can give me some suggest or code?

Edit : I did this when I was just starting out in Unity, so some things may not be the best way of doing things =]

Check out all the controller scripts on the unify community 'site : http://wiki.unity3d.com/index.php/Scripts/Controllers

Even reading the touch input documents give alot of clues as how to use 2D input. Have Fun =]


Hi, I found this very interesting, so I combined @w07061828 script with my own idea.

I don’t know C# so my answer is UnityJavaScript. An I wasn’t sure how to convert mouse co-ordinates to stage position , so I have used a raycast on a surface.

To make this work, the scene needs a little setting up.

1/ Start with a new blank scene . Add a directional light facing down .

2/ Position the Camera to (0, 20, 0) . Rotate the camera to (90, 0, 0) .

3/ Create a Plane : Position (0, 0, 0) . Scale (10, 1, 10) . In the Inspector, UnTick the checkbox for Mesh Renderer (is enabled = false)

4/ Create a Prefab PathMarker , make a Sphere - Position (0, 0, 0) ; Scale (0.2,0.2,0.2) and place it in this Prefab . This is the gameObject that shows where the mouse has been dragged . (delete the Small Sphere from the stage after the Prefab has been made)

5/ Create a Sphere : Position (0, 0, 0) ; Scale (1, 1, 1).

6/ Create a new Script. Copy and paste the following code into the Script.

7/ Attach the Script to the Sphere .

8/ Drag and Drop the Prefab Small Sphere into the Script’s Inspector , where it says objectPathMarker .

That should be all set.

Play the scene. Click and drag the mouse in the scene. The array’s store 100 frames then the sphere automatically follows movement, or follows movement if the mouse button is released. Press r to get a print of the current store array’s , the other console readout is where the raycast is hitting the plane.

	private var dragging : boolean = false;
	private var moving : boolean = false;
	private var countDrag : int = 0;
	private var countMove : int = 0;
	private var mousePosition : Vector3;
	private var mousePoint : Vector3;
	private var pointCurrent : Vector3;
	private var pointStore : Vector3;
	private var posStoreX : Array = [];
	private var posStoreZ : Array = [];
	private var arrayPathMarker : GameObject[] = new GameObject[100];
	var objectPathMarker : GameObject;
	
	function Start () {
		dragging = false;
	}
	
	function Update () {
		// note - raycast needs a surface to hit against 	
		var rayHit : RaycastHit;
		if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit)) {
			// where did the raycast hit in the world - position of rayhit
			if (dragging) {print ("rayHit.point : " + rayHit.point + " (mousePoint)");}
			mousePoint = rayHit.point;
	
			if (Input.GetMouseButtonDown(0)) {
				OnTouchBegin(mousePoint);
			}
			else if (Input.GetMouseButton(0)) {
				OnTouchMove(mousePoint);
			}
			else if (Input.GetMouseButtonUp(0)) {
				OnTouchEnd(mousePoint);
			}
		}
	}
	
	function OnTouchBegin (pointCurrent : Vector3) {
		countDrag = 0;
		posStoreX.Clear();
		posStoreZ.Clear();
		AddSplinePoint(pointCurrent);
		dragging = true;
		moving = false;
	}
	
	function OnTouchMove (pointCurrent : Vector3) {
		if ((dragging) && (countDrag < 100)) {
			print("countDrag " + countDrag);
			AddSplinePoint(pointCurrent);
		} else {
			dragging = false;
			moving = true;
		}
	}
	
	function OnTouchEnd (pointCurrent : Vector3) {
		dragging = false;
		moving = true;
	}
	
	function AddSplinePoint (pointStore : Vector3) {
		// store co-ordinates
		posStoreX[countDrag] = pointStore.x;
		posStoreZ[countDrag] = pointStore.z;
		
		// show path : Instantiate and load position into array as gameObject
		arrayPathMarker[countDrag] = Instantiate(objectPathMarker, Vector3(pointStore.x, 0, pointStore.z), transform.rotation);
		print (arrayPathMarker[countDrag].transform.position);
		
		// next position
		countDrag ++;
	}
	
	function FixedUpdate () {
		// move gameObject
		if (moving) {
			// remove path marker
			Destroy (arrayPathMarker[countMove]);
			
			// move gameObject along path
			transform.position = Vector3(posStoreX[countMove], 0, posStoreZ[countMove]);
			
			// next position
			countMove ++;
			if (countMove >= posStoreX.length) {moving = false;} // stop at end of path
		} else {
			countMove = 0;
		}
		
		if (Input.GetKeyDown("r")) {print ("posStoreX "+posStoreX+" : posStoreZ "+posStoreZ);}
	}

I have made a guide for this. Just keep in mind that I am a noob, this is just to help out other noobs :slight_smile:

Make a Simple AirCraft Controller Game in Unity