3

I create an unity3d application that loads prefab and move it. I can load a cube prefab using world coordinate.I want to move this object to mouse click position. To do this work, I use below code. My object doesn't move anywhere.

public GameObject[] model_prefabs;

void Start () {
    //for (int i = 0; i < 1; i++) {
    int i = 0;
        Instantiate (Resources.Load ("Cube"), new Vector3 (i * 1.8F - 8.2f, 0, 0), Quaternion.identity);
    //}
}

void Update() {

    if (Input.GetKey("escape"))
        Application.Quit();

    if (Input.GetMouseButtonDown (0)) {

        Debug.Log ("mouseDown = " + Input.mousePosition.x + " " + Input.mousePosition.y + " " + Input.mousePosition.z);
        Plane p = new Plane (Camera.main.transform.forward , transform.position);
        Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
        float d;
        if (p.Raycast (r, out d)) {
            Vector3 v = r.GetPoint (d);
            //return v;
            Debug.Log ("V = " + v.x + " " + v.y + " " + v.z);
            transform.position = v;
        }
        else {
            Debug.Log ("Raycast returns false");
        }
    }
}

I convert from mouse click positions to world coordinates. They looks like suitable.

mouseDown = 169 408 0
V = -5.966913 3.117915 0

mouseDown = 470 281 0
V = -0.1450625 0.6615199 0

mouseDown = 282 85 0
V = -3.781301 -3.129452 0

How can I move this object?

Machavity
  • 30,841
  • 27
  • 92
  • 100
zakjma
  • 2,030
  • 12
  • 40
  • 81

3 Answers3

1

Right now it looks like you are moving the GameObject that your script is attached to, instead of the GameObject you created. There are two ways to accomplish this.

  1. You can move everything from the if(MouseButtonDown(0)) statement to a script that is attached to your Cube prefab. But then every one of those prefabs you spawned would move to the same place.

  2. You can add a variable GameObject currentObject; then in you Start() function say currentObject = Instantiate (Resources.Load ("Cube"), new Vector3 (i * 1.8F - 8.2f, 0, 0), Quaternion.identity); and in your update function write currentObject.transform.position = v;

The_Guy
  • 108
  • 8
0

I use below code. It works for me.

void Start () {
    for (int i = 0; i < 3; i++) {
        gO[i] = Instantiate (Resources.Load ("Cube"), new Vector3 (i * 1.8F - 8.2f, 0, 0), Quaternion.identity) as GameObject;
    }
}

void Update() {

    if (Input.GetKey("escape"))
        Application.Quit();
#if UNITY_EDITOR
    if (Input.GetMouseButtonDown (0)) {
        Debug.Log ("mouseDown = " + Input.mousePosition.x + " " + Input.mousePosition.y + " " + Input.mousePosition.z);
        Plane p = new Plane (Camera.main.transform.forward , transform.position);
        Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
        float d;
        if (p.Raycast (r, out d)) {
            Vector3 v = r.GetPoint (d);

            for (int i = 0; i < 3; i++) {
                gO[i].transform.position = v;
                v.y = v.y - 2f;
            }
        }
    }
#endif
zakjma
  • 2,030
  • 12
  • 40
  • 81
0

You can use this. Just check which prefab is active.

public GameObject activePrefab;
Vector3 targetPosition;

void Start () {

    targetPosition = transform.position;
}
void Update(){

    if (Input.GetMouseButtonDown(0)){
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit)){
            targetPosition = hit.point;
            activePrefab.transform.position = targetPosition;
        }
    }
}
kagkar
  • 469
  • 5
  • 15