Search Unity

Drag and Drop tagged object only C#

Discussion in 'Scripting' started by Holocene, Nov 27, 2015.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi all,

    I'm using the following drag and drop script- which works perfectly fine.
    What I'd like to do is modify it so it only works with a particular tagged object.
    I tried replacing line:

    target = hit.rigidbody.gameObject;

    with

    target = hit.rigidbody.gameObject.tag."test";

    But it won't work. Any ideas how I can make this script only recognize this tagged object?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class DragAndDrop : MonoBehaviour
    4. {
    5.     private bool _mouseState;
    6.     private GameObject target;
    7.     public Vector3 screenSpace;
    8.     public Vector3 offset;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         // Debug.Log(_mouseState);
    19.         if (Input.GetMouseButtonDown (0)) {
    20.             RaycastHit hitInfo;
    21.             target = GetClickedObject (out hitInfo);
    22.             if (target != null) {
    23.                 _mouseState = true;
    24.                 screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
    25.                 offset = target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
    26.             }
    27.         }
    28.         if (Input.GetMouseButtonUp (0)) {
    29.             _mouseState = false;
    30.         }
    31.         if (_mouseState) {
    32.             //keep track of the mouse position
    33.             var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
    34.             //convert the screen mouse position to world point and adjust with offset
    35.             var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
    36.             //update the position of the object in the world
    37.             target.transform.position = curPosition;
    38.         }
    39.     }
    40.  
    41.  
    42.     GameObject GetClickedObject (out RaycastHit hit)
    43.     {
    44.         GameObject target = null;
    45.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    46.         if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
    47.             target = hit.rigidbody.gameObject;
    48.            
    49.                                
    50.         }
    51.         return target;
    52.     }
    53. }
     
  2. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
  3. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    Think about what you were trying to do - target = hit.rigidbody.gameObject.tag."test" doesn't make any sense. So much so that it shows a complete lack of basic programming knowledge; I would therefore highly suggest that you do some basic programming tutorials first, before getting started with Unity. It will save you so much time and frustration if you actually know what you're doing.

    To answer your question though, Physics.Raycast() has an overload which allows you to filter using a layer mask.

    Code (CSharp):
    1. if (Physics.Raycast (ray.origin, ray.direction * 10, out hit, Mathf.Infinity, LayerMask.GetMask("test"))) {}
     
  4. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Perfect, thanks for your help, Thomas!
    You got the tutorial suggestion right too :)