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.
#pragma strict
private var object : Transform;
var grabbed : Transform;
var grabDistance : float = 10.0f;
var grabLayerMask : int;
var grabOffset : Vector3; //delta between transform transform position and hit point
var useToggleDrag : boolean; // Didn't know which style you prefer.
// public var tag: string;
function Update () {
if (useToggleDrag){
UpdateToggleDrag();
} else {
UpdateHoldDrag();
}
}
// Toggles drag with mouse click
function UpdateToggleDrag () {
if (Input.GetMouseButtonDown(0)){
Grab();
} else {
if (grabbed) {
Drag();
}
}
}
// Drags when user holds down button
function UpdateHoldDrag () {
if (gameObject.tag != "Props"){
if (Input.GetMouseButton(0)) {
if (grabbed){
Drag();
} else {
Grab();
}
} else {
if(grabbed){
//restore the original layermask
grabbed.gameObject.layer = grabLayerMask;
}
grabbed = null;
}
}
}
function Grab() {
if (gameObject.tag != "Props"){
if (grabbed){
grabbed = null;
} else {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)){
grabbed = hit.transform;
if(grabbed.parent){
grabbed = grabbed.parent.transform;
}
//set the object to ignore raycasts
grabLayerMask = grabbed.gameObject.layer;
grabbed.gameObject.layer = 2;
//now immediately do another raycast to calculate the offset
if (Physics.Raycast(ray, hit)){
grabOffset = grabbed.position - hit.point;
} else {
//important - clear the gab if there is nothing
//behind the object to drag against
grabbed = null;
}
}
}
}
}
function Drag() {
if (gameObject.tag != "Props"){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)){
grabbed.position = hit.point + grabOffset;
}
}
}
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.