- Home /
Finding the layer of an object
Hi, I'm making a game where you can manipulate the game environment to proceed. This will involve being able to move small objects around, and the ability to turn on or off bigger objects using their switches. To keep it simple for the player, this will be done with the same button. However, for me, this means I have to distinguish whether the highligheted option is a moveable or a switchable object. I wanted to do this with layers, but I can't figure out how to find the layer of a transform.
Here's my code:
var hasObject: boolean = false; var otherThing: Transform;
var fwd = Camera.main.transform.forward;
var hit : RaycastHit;
var LayerMask = 1 << 9;
//I previously had this as LayerMask = 2 << 9-10; Is that right? I want it to detect both layer 9 and 10 - the moveable and switchable layers
Debug.DrawRay(camera.main.transform.position, (transform.position-camera.main.transform.position) * 50, Color.red);
if (Input.GetButtonDown ("Fire2"))
{
if (!hasObject)
{ if (Physics.Raycast(camera.main.transform.position, (transform.position-camera.main.transform.position), hit, 75, LayerMask))
//If it finds an object in the layermask, said object becomes moveable. It's here I want to insert code to distinguish between the moveable and switchable layers {otherThing = hit.transform; otherThing.rigidbody.isKinematic = true; otherThing.rigidbody.detectCollisions = false; otherThing.transform.position.z = 0; otherThing.parent = transform; otherThing.Find("Glowblock").light.enabled = true; hasObject = true; } } else { otherThing.rigidbody.isKinematic = false; otherThing.rigidbody.detectCollisions = true; transform.DetachChildren(); otherThing.transform.position.z = 0; otherThing.Find("Glowblock").light.enabled = false; hasObject = false; } }
}
Regarding your layermask comment - it wasn't. You needed Layer$$anonymous$$ask = (1<<9) | (1<<10);
Your answer
![](https://koobas.hobune.stream/wayback/20220613114225im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get a list of children (in order) 1 Answer
Changing tagged objects in script 1 Answer
Hide object on key if finds object 0 Answers
Tagging Trees placed by tree paint 1 Answer
What are the Javascript compiler errors? 0 Answers