Selecting game objects with raycasts

Hi, all,

I am trying to use a raycast to select pieces of a game object and allow the user to assemble by tapping on each one. For example, I have four cubes that I want the user to select which would then instantiate a new game object.
Any thoughts?

Thanks,

Greg

You can use the script in this thread as a basis for your raycasting. You might want to keep the selected objects in an array during the selection process. The easiest way to combine objects is to create a new GameObject and then add the selected objects as its children:-

var selected: GameObject[];   // Filled by your selection process.
var newParent = new GameObject();
newParent.transform.position = <somewhere near the kids>;

for (int i = 0; i < selected.Length; i++) {
    selected[i].transform.parent = newParent.transform;
}

This is admittedly a bit vague. If you need more specifics then perhaps you can give a bit more detail about how the game will work.

Hi, Andeeee,

Thanks for taking a moment and helping me with this.
I’m using the following raycast script to identify objects named “enemy.” If the object is labeled “enemy” I want the script to remember that it just touched an “enemy” object and to do something after it touches another “enemy” immediately after.
Ie, if I had numerous objects and wanted to select three spheres out of a group of cubes. I only want the spheres to be destroyed if they’ve been touched one after another. If, for instance a sphere was selected, then a cube, nothing should happen.

Does that make any sense? Sorry, I’m finding it difficult to explain my question.

Thanks,

G

var hit : RaycastHit;
var myTransform : Transform;

function Awake() {
   myTransform = transform; // this objects transform
}

function Update () {
   for (var evt : iPhoneTouch in iPhoneInput.touches) {
      if (evt.phase == iPhoneTouchPhase.Began) {
         var ray = camera.main.ScreenPointToRay(evt.position);
         if (Physics.Raycast(ray, hit, 2000) ) {
            var hitObject : GameObject = hit.transform.gameObject;
            var hitTransform : Transform = hitObject.transform;
            if ( hitObject.tag == "enemy" ) {
               if ( myTransform.position.x == hitTransform.position.x) {       // matches x position
                  if ( myTransform.position.y == hitTransform.position.y) {   // matches y position
                     // so this is the object touched in the 2d space

  // DO SOMETHING HERE...