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);
}
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
var beingMoved : Transform;
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
for(touch in iPhoneInput.touches) {
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.
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?
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…
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");
}
}
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 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…