Can Avatar not leave an entity?


#1

Hello all,

I am working on a project in which avatars’ movements are limited in a certain range, like a zone or room. The current solution would be disabling leaveEntity function, without clear directions though. Has anyone have achieved this before? Need help out here. Any other solutions would be very helpful!!! Thank you all in advance.


#2

You can’t disable leaveEntity as a it is function it is always called by the client, if it exists in the script, when a user leaves an entity. But did you mean how can one stop user from leaving a zone?.

There is a couple of methods you can do this.

  • You could either simply add invisible entities to act as walls and add a Zone with a domain setting that disables flying / Ghosting
  • You can use the leaveEntity function as a trigger to
    - Disable user input, removing their ability to move further away, but still allowing them to go back
    - Teleport user back if they leave
    - Push the avatar back into the zone. which doesnt require as much Controller overriding

There used to a pusher script, but I can’t find it any more.
Walls or Teleporting is much easier, here is some pseudo code.

{
//[...]
leaveEntity: function (zoneId) {
    var properties = Entities.getEntityProperties(zoneId, ["position"]); // Gets position of the zone
    MyAvatar.position = properties.position;  // Teleports user to the center of the zone.
    // You can also calculate their position before leaving the zone here if you want
}
//[...]
}

Pushing the Avatar back is a tad bit more complex

{
//[...]
// Instance entity
pusher: null,
leaveEntity: function (zoneId) {
    var properties = Entities.getEntityProperties(zoneId, ["position"]); // Gets position of the zone
    // Defined here to keep within instance.
    this.pusher = function(dt){
          // Simplified impulse, pushes towards the center of the zone,
          // but you can do better math here to just push towards the bounding box dimensions.
          var rotation = Quat.lookAt(MyAvatar.position, properties.position, Vec3.UP);
          var direction = Vec3.normalize(Quat.getFront(rotation));
          var distance = Vec3.distance(MyAvatar.position, properties.position);       
          var thrust = Vec3.multiply(direction, 4000); // Not sure on this value, just halfed the value I use for my Hook gun
          MyAvatar.addThrust( Vec3.multiply(thrust, dt)); // push by update thread time between frames
    };
    Script.update.connect(this.pusher);
},
enterEntity: function(zoneId){
    if(this.pusher != null){
        Script.update.disconnect(this.pusher); // Disable the reversing / calculation of the user direction
        this.pusher = null;
    }
}

//[...]
}

Hopefully that helps.