Search Unity

problem with inventory system and mouse position

Discussion in 'Scripting' started by incitement1, Apr 28, 2013.

  1. incitement1

    incitement1

    Joined:
    Apr 28, 2013
    Posts:
    8
    i have an inventory system with java script
    i wrote this code to take my object from the inventery system to my scene.

    function ReleaseObjectFromInventory(t : int){//releases the object at index t from the inventory.
    objectsInBag[t].active = true;
    objectsInBag[t].transform.localPosition += Vector3(13,0,0.5);
    objectsInBag[t].transform.parent = null;
    mouseTextureHandler.ReleaseCursorIcon();
    objectsInBag[t] = null;
    }


    the problem is when i have more than one object in the inventory system and a like to release them all. they appear in the same position
    make by defaukt Vector3(13,0,0.5). i don't now how to make an object released at the mouse position instead of the default Vector3.

    can anyone help me please. thankyou
     
    Last edited: Apr 28, 2013
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    use this

    Code (csharp):
    1. Vector3 spawnObjThere = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. spawnObjThere.y = 0;   // reset to 0 to prevent little offset
    3. // instantiate your object here or whatever u want to do
    The object should spawn at your mouseposition on your terrain
     
    Last edited: Apr 28, 2013
  3. incitement1

    incitement1

    Joined:
    Apr 28, 2013
    Posts:
    8
    thank you SlyRipper i add the whole script. can you corrected to me please with the line you add it. thank you
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    do you mean where to put the snippet of code? probably in function Update after checking for if(Input.GetMouseButtonDown(0)) or the input you use to release an object
     
  5. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    As jister said, you want to paste this snippet below the part where you release the mouse key to drop the item in the world/scene, then get the mouseposition and instantiate ur unity/object/item/whatever..
     
  6. incitement1

    incitement1

    Joined:
    Apr 28, 2013
    Posts:
    8
    thank you i found a solution. but ia have just more on question please. i would like to add some object by default in my inventor. so player when he start the game can found it when opened the inventor. i don't found any idea for who to do it. my inventor just work fine with object a add it with the game.