Search Unity

Select and Drag a Point (Vector) in the scene to manipulate a public Vector

Discussion in 'Scripting' started by steveauch, Sep 15, 2016.

  1. steveauch

    steveauch

    Joined:
    Jul 11, 2016
    Posts:
    5
    Trying to implement a non-rigid object placement/manipulation interface, where we can select points associated with the object in the Scene, and drag them to change the values of public variables associated with the object, as defined in a script.
    The simplest example would be a line object with two points, made public in an associated script. We want to be able to click and drag these points individually in the Scene to modify the public variables, and redraw the line to go between these points. Similar to how the transform of an object can be manipulated in the Scene.
    I can't seem to get my search criteria right to find the relevant documentation.
    Thanks in advance....
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You have two choices:
    Put colliders on all your objects. You can put these colliders in their own physics layer and make them non interactable with everyting including themselves. Then you can use the a physics.Raycast like this:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hit;
    5.  
    6.         if (Physics.Raycast(ray, out hit, 100))
    7.             Debug.Log("You clicked on: " + hit.collider.gameObject.name);
    8.     }
    if you don't want to add colliders to every object, your type of camera matters:
    If its orthographic you can very easily just check the bounding box of all your objects and see if the mouse's x,y position is inside one of them when you click.

    If you have a perspective camera, your going to have to take the FOV into account.
    Basically the x,y location in world space from your mouse's x,y screen space will be different depedning on the depth (whereas with orthographic its constant). Basically what you do is create a Vector3 starting at the "screen" with your mouse's x,y, z position and move it forward , adjusting the x,y based on your Z depth and seeing if you intersect any of your object's bounding boxes. This is pretty much what Unity's Colliders and Physics Raycast handle for you.
     
  3. steveauch

    steveauch

    Joined:
    Jul 11, 2016
    Posts:
    5
    Thanks Takatok.
    Either you posted an answer to another question, I phrased my question really badly.... or I have no idea what you are talking about.
    While creating my scene, I want to be able to drag the endpoints of an object (eg a line/box) to change that objects transform. To do this I thought I could put two global vectors in the script (eg front and back) which then are revealed in the Inspector. I can change these values manually, and the script can then modify the transform position to have the object extend from one point to the other point. This works. To make it easier to manipulate, I would like to modify these vectors in the scene editor (ie with a mouse click/drag) the same way you can move the objects transform (you know, with the boxes and arrows etc).
     
  4. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    89
    You can have the endpoints defined by two empty gameobjects as children of the object. You can modify their position in the scene and get their transform.position in the script so they act like the two vectors (front and back).
     
    Last edited: Sep 16, 2016
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Yes it was my misunderstandng. I was thinking you wanted to move entire objects around (while playing the game). I am not very familiar with coding to modify editor behaviour.