touch and move

I have an object being spawned randomly on the iphone screen. I want to be able to touch one item at a time and move(collide) it onto another object. I have my following script, when i touch on the screen all the objects are moved together, how can I touch and move one object at one time.

function Update () {
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     for(touch in iPhoneInput.touches) {
     	if (Physics.Raycast (ray, hit)) {
	     if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
	          transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 9));
	     	
	     	if(hit.collider.gameObject.tag =="Player" )
	     	
	     	{
	     		Debug.Log("TOUCHED");

	     	}
	     }
	}
     }
}

The following is the spawning object

function Spawn()
{
	var obj : GameObject = prefab[Random.Range(0, prefab.length)];
	var pos: Transform = spawn[Random.Range(0, spawn.length)];
	
	spawning = true;
	timer = 0;
	Instantiate(obj, pos.position, pos.rotation);
}

Thank you in advance.

You need to make sure that once an object has been “touched” then none of the other objects are allowed to be touched by the same finger.

Make sure this script isn’t on every item that can be touched and only exists on 1 single game object. I didn’t test it so you’ll need to make sure it compiles :slight_smile:

var beingMoved : Transform;

function Update () {

var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
for(touch in iPhoneInput.touches) {

if (Physics.Raycast (ray, hit)) {

if (touch.phase == iPhoneTouchPhase.Began) {

beingMoved = hit.collider.transform;

}

if(touch.phase == iPhoneTouchPhase.Moved) {

beingMoved.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 9));

if(hit.collider.gameObject.tag ==“Player” )

{
Debug.Log(“TOUCHED”);

}
}
}
}
}

Thanks trooper… the script your provided works fine. i think i was not clear what im doing.
I am creating the same object to be respawn many times. So there will be so many object of the same type. I need to touch any of the object and move it one by one.
The script is attached on that game object only. When i touch all the objects follows together (this is not what im doing). No matter where I touch all the objects will overlap each other.

Ok, attach that part of the update script to an empty game object that doesn’t get spawned and it should move them around as you’d expect.

The problem is that the update is running one every frame for every object, you want it to run only once per frame.

I have attached the script to the empty game object, and added the game object to the script. Now it moves only that one object. The others that spawn not able to be touched.
Is there a way to detect which object is being touched and the lock it till the finger is removed?

Maybe try this instead… make sure this is attached to a single game object and all the other spawned objects don’t have scripts.

var beingMoved : Transform;

function Update () {
[INDENT]
var hit : RaycastHit;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

for(touch in iPhoneInput.touches) {
[INDENT]
if(touch.phase == iPhoneTouchPhase.Moved  beingMoved != null) {

[INDENT]beingMoved.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 9));
[/INDENT]
}

elseif (touch.phase == iPhoneTouchPhase.Began)
{

[INDENT]if (Physics.Raycast (ray, hit)) {

[INDENT]beingMoved = hit.collider.transform;[/INDENT]
}[/INDENT]

}
elseif (touch.phase == iPhoneTouchPhase.Ended)
{

[INDENT]beingMoved = null;
[/INDENT]
}
[/INDENT]
}

}[/INDENT]
}

If i do this code. I can only move the object that is added to beingMoved. Once the finger is removed it cant be touched anymore.This is fine
but how about the other spawned object. I still cant touch them. If you see my spawn code above I have unlimited spawn objects. I need to be able to touch each of them.
I am doing something wrong here. Please guide.

HELLO !i have a question I’m Farley new to unity i have a lot of game design experience and can create some sick things if i new were certain features were located in unity.would you be able to help me please I’m having a hard time finding simple things, and i’m becoming very frustrated i would rly appreciate it…

Hi Farley,
Im also new to Unity and learning to use it. Post your question if I know I will help you out. We all are helping each other here…

Try this package, my unity remote was playing up but I think it does what you want.

456839–15999–$MoveOnObject.unitypackage (10.5 KB)

Thanks a million trooper… this what i am looking for… i have been working on this for two days…
n now i know what i did different… i attached the script to empty game object and added the transform object to it.
ok… I have another question now…
when i drag a cube over a capsule it collides, once it does that i want the cube to be destroyed… i did the following but the whole game is destroyed.

function OnCollisionEnter(collision: Collision)
{
	if(collision.gameObject.tag == "Player") 
    {
       Destroy(collision.gameObject); //Destroy Current Gameobject that is being touched.
         Debug.Log("Destroy");
    }
}

Need to debug this problem, do this first:

Debug.Log("Destroy: " + collision.gameObject.name);

and make sure that your level doesn’t have the tag of “Player”. If the script your showing is on all cubes then it should work as expected.

http://www.youtube.com/watch?v=6ky9f5txC84 i wana make such cude can rotated and shift, bt in my case i hv number of cude Plz guide

I did that. It destroy my object that it collides with.
Where my cube is the object i touch and move to a capsule, the capsule gets destroyed. I changed the code to
if(collision.gameObject.tag == “MoveObject”)
but nothing happens.

If I would like to destroyed the object, other then doing collision is there other way to perform this action.

You want the object you are moving to destroy or the object that you are hitting to destroy?

The object that i am moving to be destroyed.

You need a way of turning the collision scripts on and off depending if they are being moved or not. You only want the collision script active on the object you are moving, replace “CollisionScript” with your own script name:

var beingMoved : Transform;

function Update () {

var hit : RaycastHit;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

for(touch in iPhoneInput.touches) {

if(touch.phase == iPhoneTouchPhase.Moved  beingMoved != null) {
beingMoved.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 9));
}

elseif (touch.phase == iPhoneTouchPhase.Began)
{
if (Physics.Raycast (ray, hit)) {

if (beingMoved != null)
{
beingMoved.GetComponent("CollisionScript").enabled = false;
}

beingMoved = hit.collider.transform;
beingMoved.GetComponent("CollisionScript").enabled = true;
}
}
elseif (touch.phase == iPhoneTouchPhase.Ended)
{
beingMoved = null;
}
}

}
}

You need this:

function Start()
{

this.enabled = false;

}

function OnCollisionEnter(collision: Collision)
{
	if(collision.gameObject.tag == "Player") 
    {
       Destroy(this); //Destroy Current Gameobject that is being touched.
    }
}

I did as the code, but once i drag the cube over the capsule… it does not get destroyed but am i able to move anything on the scene, the capsule, the plane…

The OnCollisionEnter script is on each cube isnt it?

No I dont. My cubes are randomly generated on the scene. Do I need to attach the script on the cube gameObject?