Studio GuideWorld SDK Guide
Log In

Camera Collision Layer

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 Layer6 ~ 7 are Unity's default layers.


  1. Select the object you want the camera to apply collision judgment to, and then select the layer you set up.


  1. 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.

  1. 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

  1. You can specify Camera Collision dynamically at runtime.
  2. 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.
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;
    }
 
}
  1. 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

❗️

Caution

  • 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.