iPhone :: Moving Multiple Spawn Objects Independantly

Hi

First, thanks to anyone who has posted in these forums. It’s been a week for me now and I’ve learned so much and have borrowed code, played with it, tweaked here and there, and have drunken about 100 cups of coffee - and have enjoyed every moment.

So, here is my current problem. Have been sitting on this problem for a day now and searched the forums, but I can’t seem to find a viable solution.

Action 1:
I have 3 Prefab Objects spawning from one spawn point randomly. This works fine.

Action 2 - PROBLEM 1.
I want to be able to move each of these spawned objects to any position on screen. I have the drag/move script working fine for this. However, and this is the question/problem, when i touch a spawned object, it moves as planned, but then as soon as another object spawns, it attaches itself to the object I’m currently moving, if you get the drift.

PROBLEM 2
If i click on a place on screen anywhere BUT on the object itself and drag on the screen with the finger it will jump to that position (start of drag). Trying to find a solution now for it (if it’s obvious to your more-qualified eyes in the code above please let me know :slight_smile: )

Is it something to do with the line “iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved)” ? is this saying that until the finger starts moving, that’s when it will move the object? (but it does so from anywhere on screen rather than from the object itself.)

//Drag the Spawned Object Enemy across 3D plane======

//var enemyselect : Transform; (NB: I used to have this in there until i realised it had no effect. In the inspector on the prefab object i would choose the prefab itself - but i need a way for the touch to register ONLY on each spawned object individually.)

var hit : RaycastHit;

function Update () {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	for (touch in iPhoneInput.touches) {
		if (Physics.Raycast (ray, hit, 100)) {
			if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
		
				transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x,touch.position.y,10));
			}
		
		}
	}
}

Thanks for any advice you can give. And remember, I’m a complete noob!

I have a third question now relating to the above as well.

The movement of the object along x and y is fine - so long as the camera position is facing the scene (e.g. at 0,0,0). However, I need a camera tilt (rotation 70 to the plane at 0,0,0). Doing this throws the above out of whack - if you drag the object to the bottom of screen you lose the object under the ground. Is there somewhat to measure the ray to the ground plane from camera?

I’m thinking i need to cast a ray to the ground object - I’ve been reading up but not sure yet how to implement it as part of the above code.

Anyway, I’ve hogged this thread long enough…

Thanks again

The raycast is a good way to detect and identify the object you are moving, but you don’t appear to be using it for anything in particular here. The key ingredient is the RaycastHit object. This is set by Physics.Raycast to give you some information about the object that was hit. You can check if the hit object is the same as the current object (ie, the one with the script) using its transform property:-

if (Physics.Raycast (ray, hit, 100)  (hit.transform == transform)) {
   ...
}

I think this should fix problems 1 and 2.

As for problem 3, if you have an object in the background (a plane, say), then you can detect where the ray hits it using the point field of the RaycastHit object. The point is the location in world space where the ray makes contact with the object it hits.

Regarding points 1 2 - would your suggestion still hold true considering that the objects on screen are clones of the prefab (and many of them)… EG There could be 4 x “Noob(Clone)” objects on the screen at the same time.

I’m exploring the use of (touch.fingerId) but not sure how to implement or if it would work in this scenerio.

But there has to be a way - if you want an idea of the movement i’m looking for on screen check out the game called Fling (there’s a free version available).

Yes, the transform is unique for each individual object.

Thanks Andee

I’ve managed to get all of the above working after tinking with another script I found in the forums. Really appreciate all the advice you give - I’m learning alot, Sensei

:o :wink:

Now for my next post elsewhere regarding sensitivity of touches :slight_smile:

And I am learning much from you, grasshopper… :wink: