The board administrator and developer will be on vacation until the end of July. Therefore replies will be slower than usual.

Teleportation Item

User avatar
PeterBibinin
Posts: 178
Joined: Mon Oct 24, 2016 10:55 pm

Teleportation Item

Postby PeterBibinin » Thu Dec 01, 2016 3:56 pm

First create in scene GameObject and attach this script this will by Position Manager:

Code: Select all

using UnityEngine;
using System.Collections;


    public class MyPositionManager : MonoBehaviour
    {
        private static MyPositionManager _instance;

        public static MyPositionManager instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = GameObject.FindObjectOfType<MyPositionManager>();
                }

                return _instance;
            }
        }

        public Transform playerResetPosition;
}


Now create GameObject and place it somewere in scene <-- This will be place where player will be teleported.
And attach to Transform in MyPositionManager.

Now we need create custom item type:

Code: Select all

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Devdog.General;
using Devdog.InventoryPro;


   public class TeleportItem : InventoryItemBase {

    private MyPositionManager positionManager;

       

        public override LinkedList<ItemInfoRow[]> GetInfo()
      {
         var basic = base.GetInfo();
         //basic.AddAfter(basic.First, new InfoBoxUI.Row[]{
         //    new InfoBoxUI.Row("Restore health", restoreHealth.ToString(), Color.green, Color.green),
         //    new InfoBoxUI.Row("Restore mana", restoreMana.ToString(), Color.green, Color.green)
         //});


         return basic;
      }

      public override void NotifyItemUsed(uint amount, bool alsoNotifyCollection)
      {
         base.NotifyItemUsed(amount, alsoNotifyCollection);

         PlayerManager.instance.currentPlayer.inventoryPlayer.stats.SetAll(stats);
      }

      public override int Use()
      {
         int used = base.Use();
         if (used < 0) // Whoops! Anything below 0 is a failed action.
            return used;

         if (currentStackSize <= 0)
            return -2;
        // Do something specific...

   // Teleport player
        var player = PlayerManager.instance.currentPlayer;
       
        player.transform.position = MyPositionManager.instance.playerResetPosition.position;
        player.transform.rotation = MyPositionManager.instance.playerResetPosition.rotation;

        // Do something with item, this will remove item from inventory
        currentStackSize--; // Remove 1



         NotifyItemUsed(1, true);
         return 1; // 1, the item was used, but no items were used (stack decrease) in the process
      
      }
   }


have fun ;]
Waiting for sunrise
----------------------------------------------
Try make world beter ;]
----------------------------------------------
BAZINGA !

Return to “Share custom components”

Who is online

Users browsing this forum: No registered users and 1 guest