Search Unity

Code Question

Discussion in 'Scripting' started by Muralidaran, Jan 30, 2015.

  1. Muralidaran

    Muralidaran

    Joined:
    May 2, 2014
    Posts:
    93
    Hello. I was just looking for some help with a piece of code. I am using the code below to move objects with the mouse but I want the code to ignore objects with the tag "Props". Everything moves like it is supposed to but the ignore part isn't working.
    Code (CSharp):
    1. #pragma strict
    2.  
    3. private var object : Transform;
    4. var grabbed : Transform;
    5. var grabDistance : float = 10.0f;
    6. var grabLayerMask : int;
    7. var grabOffset : Vector3; //delta between transform transform position and hit point
    8. var useToggleDrag : boolean; // Didn't know which style you prefer.
    9. // public var tag: string;
    10. function Update () {
    11.      if (useToggleDrag){
    12.          UpdateToggleDrag();
    13.      } else {
    14.          UpdateHoldDrag();
    15.      }
    16. }
    17. // Toggles drag with mouse click
    18. function UpdateToggleDrag () {
    19.      if (Input.GetMouseButtonDown(0)){
    20.          Grab();
    21.      } else {
    22.          if (grabbed) {
    23.              Drag();
    24.          }
    25.      }
    26. }
    27. // Drags when user holds down button
    28. function UpdateHoldDrag () {
    29. if (gameObject.tag != "Props"){
    30.      if (Input.GetMouseButton(0)) {
    31.          if (grabbed){
    32.              Drag();
    33.          } else {
    34.              Grab();
    35.          }
    36.        
    37.      } else {
    38.          if(grabbed){
    39.              //restore the original layermask
    40.              grabbed.gameObject.layer = grabLayerMask;
    41.          }
    42.          grabbed = null;
    43.      }
    44. }
    45. }
    46. function Grab() {
    47.      if (gameObject.tag != "Props"){
    48.      if (grabbed){
    49.         grabbed = null;
    50.      } else {
    51.          var hit : RaycastHit;
    52.          var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    53.          if (Physics.Raycast(ray, hit)){
    54.              grabbed = hit.transform;
    55.              if(grabbed.parent){
    56.                  grabbed = grabbed.parent.transform;
    57.              }
    58.              //set the object to ignore raycasts
    59.              grabLayerMask = grabbed.gameObject.layer;
    60.              grabbed.gameObject.layer = 2;
    61.              //now immediately do another raycast to calculate the offset
    62.              if (Physics.Raycast(ray, hit)){
    63.                  grabOffset = grabbed.position - hit.point;
    64.              } else {
    65.                  //important - clear the gab if there is nothing
    66.                  //behind the object to drag against
    67.                  grabbed = null;
    68.              }
    69.          }
    70.      }
    71. }
    72. }
    73. function Drag() {  
    74. if (gameObject.tag != "Props"){
    75.      var hit : RaycastHit;
    76.      var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    77.      if (Physics.Raycast(ray, hit)){    
    78.          grabbed.position = hit.point + grabOffset;
    79.      }
    80. }
    81. }

    I an trying to use: if (gameObject.tag != "Props"): to ignore the Props, I think I may just have it in the wrong places.
    Thanks for the help.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    gameObject.tag references the tag of the object your script is attached to. You're probably looking for grabbed.tag.
     
  3. Muralidaran

    Muralidaran

    Joined:
    May 2, 2014
    Posts:
    93
    Thanks Baste. I tried that but it didn't work right, so I moved the tag check up to the update and it worked, sorta, but kept throwing an "Instance not assigned error.

    Any more thoughts?