CREATE YOUR WORLD
Players & Characters: Advanced
Camera Collision Layer
8min
overview zepetocamera created through zepetoplayers can control physical collisions between the camera and game objects through the camera collision setting camera collision is when the camera collides with an object in the scene if camera collision is set, when the camera collides with an object, it will not be able to move in front of that object, causing the camera to stay in front of the object instead of through it let's take a look at the following two examples in the screen on the left, camera collision is not set, allowing the camera to move freely without being affected by objects around the character or the floor on the right, camera collision is enabled, so the camera will not be able to penetrate walls and floors when it moves by enabling camera collision, the camera's movement is physically affected, meaning that locations that the player cannot see cannot be illuminated by the camera, which allows for a more realistic depiction setting up zepetoplayers camera collision 1\) you can set what collision the camera detects per layer to set or add a user layer, press the layer drop down menu in the inspector window and select add layer the layers that a user can add can be numbered layer3 and layer6 or higher layer0 2 and layer4 5 are unity's default layers 2\) select the object you want the camera to apply collision judgment to, and then select the layer you set up 3\) now, in the camera part of zepetoplayers, you can select the layer you want to register as a collision by pressing the drop down button for collision check the layers you have added 4\) now press the play button to run the runtime and you will see that the zepeto camera moves through the floor and walls without breaking through them setting it as a script at runtime you can specify camera collision dynamically at runtime below is a script to set the collisionlayer number registered as a public variable as a layer mask at runtime the key is to specify the layer mask to be selected in zepetoplayers instance cameradata layer cameracollisionmanager import { layermask } from 'unityengine'; import { spawninfo, zepetoplayers } from 'zepeto character controller' import { zepetoscriptbehaviour } from 'zepeto script' import { worldservice } from 'zepeto world'; export default class cameracollisionmanager extends zepetoscriptbehaviour { // declare a public member to store an array of collision layer numbers public collisionlayer number\[]; start() { // create localplayer zepetoplayers instance createplayerwithuserid(worldservice userid, new spawninfo(), true); // add an event listener for when the local player is added to the scene zepetoplayers instance onaddedlocalplayer addlistener(() => { // set the camera collision layer to the new layer mask zepetoplayers instance zepetocamera collisionlayer = this setnewlayermask(); }); } // method to create and set a new layermask based on the collision layers private setnewlayermask() layermask { let layermaskvalue = 0; // iterate through the collision layers array and update the layer mask value this collisionlayer foreach(layernumber => { layermaskvalue |= 1 << layernumber; }); // create a new layermask and set its value to the calculated layer mask value let newlayermask layermask = new layermask; newlayermask value = layermaskvalue; return newlayermask; } } 3\ script description in order to script a layer and apply it to the camera, we need to use a layermask the layermask will have a value entered through bitwise operations the layermask is represented as an integer value, where each bit indicates whether the layer is included or not for example, if the layermask has a value of 5 (00000101 in binary), we can say that layer 0 and layer 2 are included layermaskvalue |= this bitwise operation combines the existing layermaskvalue and the newly calculated value in an or operation if the existing layermaskvalue is 00000001 in binary, and the newly calculated value is 00000100, oring the two values will result in 00000101 in binary this ensures that both layer 0 and layer 2 are included in the layermask 📘 unity layer mask https //docs unity3d com/2020 3/documentation/scriptreference/layermask html https //docs unity3d com/2020 3/documentation/scriptreference/layermask html in the case of enhancedcamera, the player layer is not set to the camera’s collision layer to prevent the camera from intersecting with the character