Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Transform Position of all Objects from a List

Discussion in 'Scripting' started by sarges78, Dec 4, 2016.

  1. sarges78

    sarges78

    Joined:
    Nov 24, 2016
    Posts:
    4
    Hi,

    can someone please enlighten me what i do wrong? Im looking at this for quiet some time now and i dont get it.

    I have a GameObject that can be dragged around. When i now press the F key i instantiate a second game object and add the new instantiatet and the already existing one into a list.

    I now want both objects in that list to move while either one is dragged relative to their position. If object one is at 0,0,0 and object two is at 2,0,0 and i drag object one for example one the y axis, object two should move on the y axis aswell.

    My code just adds the drag position to the object and then resets it.

    My Object Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class dumdum : MonoBehaviour{
    6.  
    7.     GameController controller;
    8.  
    9.     void Start()
    10.     {
    11.         controller = GameObject.Find("GameController").GetComponent<GameController>();
    12.     }
    13.  
    14.     void OnMouseDown()
    15.     {
    16.         controller.OnMouseDown(this.gameObject);      
    17.     }
    18.    
    19.     void OnMouseDrag()
    20.     {
    21.         controller.OnMouseDrag(this.gameObject);
    22.     }
    23.    
    24.     void OnMouseUp()
    25.     {
    26.         controller.OnMouseUp(this.gameObject);      
    27.     }
    28. }
    29.  
    And the controller code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GameController : MonoBehaviour {
    6.    
    7.     Vector3 distance;              
    8.     float posX;                    
    9.     float posY;              
    10.  
    11.     List<GameObject> moveUs;
    12.  
    13.     public GameObject lulu;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         moveUs = new List<GameObject>();      
    19.     }
    20.  
    21.     public void OnMouseDown(GameObject dumdum)
    22.     {      
    23.         distance = Camera.main.WorldToScreenPoint(dumdum.transform.position);
    24.         posX = Input.mousePosition.x - distance.x;
    25.         posY = Input.mousePosition.y - distance.y;      
    26.     }
    27.  
    28.     public void OnMouseDrag(GameObject dumdum)
    29.     {      
    30.         Vector3 currentPosition = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, distance.z);
    31.         Vector3 worldPosition = Camera.main.ScreenToWorldPoint(currentPosition);
    32.         dumdum.transform.position = worldPosition;
    33.        
    34.         if (moveUs.Count > 1)
    35.         {
    36.             for (int i = 0; i < moveUs.Count; i++)
    37.             {              
    38.                 if (moveUs[i] != null)
    39.                 {
    40.                     moveUs[i].transform.position = worldPosition;
    41.                 }              
    42.             }
    43.         }      
    44.  
    45.         if (Input.GetKeyUp(KeyCode.F))
    46.         {          
    47.             moveUs.Add(dumdum.GetComponent<GameObject>());
    48.             moveUs.Add(Instantiate(lulu, new Vector3(0, 0, 0), Quaternion.identity) as GameObject);
    49.         }
    50.     }
    51.    
    52.     public void OnMouseUp(GameObject dumdum)
    53.     {
    54.         Debug.Log(" " + moveUs.Count);
    55.     }
    56. }
    57.  
    Thank you in advance!
     
  2. BitCrushed

    BitCrushed

    Joined:
    Dec 2, 2015
    Posts:
    75
    parent all objects dragged , which are in the list you mention , all to one gameobject , then unparent them again after dragging , i guess this way you wont need to iterate constantly over each element , given the oppisit solution that comes to mind.
     
  3. sarges78

    sarges78

    Joined:
    Nov 24, 2016
    Posts:
    4
    nah, thank you :) But fumbling around with parents just brings more problems then it solves. I used a parent approach before.