Advertisement
Guest User

trigvol_teleport.cs

a guest
Sep 9th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. /*
  2. This work uses content from the Sansar Knowledge Base. © 2017 Linden Research, Inc.
  3. Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/
  4. and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode).
  5. */
  6.  
  7. using Sansar.Simulation;
  8.  
  9. /// <summary>
  10. /// teleport when someone (or some thing) enters the volume the script is attached to.
  11. /// </summary>
  12. public class trigvol_teleport : SceneObjectScript
  13. {
  14.     public Sansar.Vector TP_Dest;
  15.  
  16.     public override void Init()
  17.     {
  18.         RigidBodyComponent rigidBody;
  19.         // Collision events are related to the RigidBody component so we must find it.
  20.         // See if this object has a RigidBodyComponent and grab the first one.
  21.         if (ObjectPrivate.TryGetFirstComponent(out rigidBody)
  22.             && rigidBody.IsTriggerVolume())
  23.         {
  24.             // Subscribe to TriggerVolume collisions on our rigid body: our callback (OnCollide) will get called
  25.             // whenever a character or character vr hand or dynamic object collides with our trigger volume
  26.             rigidBody.Subscribe(CollisionEventType.cTrigger, OnCollide);
  27.         }
  28.         else
  29.         {
  30.             // This will write to the script console
  31.             Log.Write("Couldn't find rigid body, or rigid body is not a trigger volume!");
  32.         }
  33.     }
  34.  
  35.     // "Collision" events are really RigidBody events, and they get RigidBodyData.
  36.     private void OnCollide(CollisionEventType EventType, Sansar.Script.ComponentId ComponentId, Sansar.Script.ComponentId HitComponentId, ObjectPublic HitObject, CollisionEventPhase phase, ControlPointType hitControlPoint)
  37.     {
  38.        
  39.         if (hitControlPoint != ControlPointType.Invalid) return; // Ignore hands
  40.  
  41.         if (phase == CollisionEventPhase.cTriggerEnter)
  42.         {
  43.             //ScenePrivate.Chat.MessageAllUsers($"Object {HitComponentId.ObjectId} has entered my volume!");
  44.             try
  45.                 {
  46.                     // Get a full API for the objet we hit, since we are a part of the scene.
  47.                     ObjectPrivate objectprivate = ScenePrivate.FindObject(HitObject.ObjectId);
  48.                     // AnimationComponents own the position of characters. To set a person's position we must do it through the animation component.
  49.                     AnimationComponent anim;
  50.                     if (objectprivate.TryGetFirstComponent(out anim))
  51.                     {
  52.                         // If there is an AnimationComponent use it to set the character's position.
  53.                         anim.SetPosition(TP_Dest);
  54.                     }
  55.                 }
  56.                 catch (System.Exception e)
  57.                 {
  58.                     // If the person is no longer in the scene then a null reference exception is thrown: just ignore it.
  59.                     if (!(e is System.NullReferenceException))
  60.                     {
  61.                         // Log other exceptions.
  62.                         Log.Write("Got exception trying to set agent position: " + e.ToString());
  63.                     }
  64.                 }
  65.         }
  66.         else
  67.         {
  68.             // HitObject might be null if the object or avatar is no longer in the scene, here we are just reporting the object id.
  69.             //ScenePrivate.Chat.MessageAllUsers($"Object {HitComponentId.ObjectId} has left my volume!");
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement