1. Hello,

    I am trying to detect cupboard position while player build.
    I just try with RaycastAll but i can't get it working, any tips?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Facepunch.Extend;
    using Newtonsoft.Json.Linq;
    using UnityEngine;namespace Oxide.Plugins
    {
        [Info("CupLimiter", "sami37", "1.0.0")]
        [Description("Limit cupboard placement")]
        class CupLimiter : RustPlugin
        {
            #region Oxide Hooks
            private FieldInfo serverinput;        void OnServerInitialized()
            {
                serverinput = typeof(BasePlayer).GetField("serverInput", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
            }        private void OnEntityBuilt(Planner plan, GameObject ent)
            {
                var cupboard = ent.GetComponent<BuildingPrivlidge>();
                if(cupboard)
                {
                    var player = BasePlayer.FindByID(cupboard.OwnerID);
                    if (player != null)
                    {
                        var input = serverinput.GetValue(player) as InputState;
                        var currentRot = Quaternion.Euler(input.current.aimAngles) * Vector3.forward;
                        Vector3 eyesAdjust = new Vector3(0f, 1.5f, 0f);
                        var rayResult = Ray(player.transform.position + eyesAdjust, currentRot);
                        if (rayResult is BaseEntity)
                        {
                            var target = rayResult as BaseEntity;
                            if (target.transform.position == cupboard.transform.position)
                                return;
                            SendReply(player, "You are not able to place Tool Cupboard here.");
                            cupboard.Kill();
                        }
                    }
                }
            }
            private object Ray(Vector3 Pos, Vector3 Aim)
            {
                var hits = Physics.RaycastAll(Pos, Aim);
                float distance = 70f;
                object target = null;            foreach (var hit in hits)
                {
                    if (hit.collider.GetComponent<BuildingPrivlidge>() != null)
                    {
                        if (hit.distance < distance)
                        {
                            distance = hit.distance;
                            target = hit.collider.GetComponent<BuildingPrivlidge>();
                        }
                    }
                }
                return target;
            }
            #endregion    }
    }
     
  2. From what I remember, OnEntityBuilt only detects things built with the building plan, that is: foundations, walls, floors, etc. If you're wanting to detect a cupboard, you should apply the same code with OwnerID but with OnEntitySpawned instead.
     
  3. Actually the code detect the placed cupboard, but not the other one in the range.
    I am trying to get existing cupboard within a range of placed cupboard.
     
  4. I'm not sure I understand, are you saying OnEntityBuilt does indeed detect cupboards? Even if it does, I'm still not entirely sure what your goal is, because your raycast seems to just find whatever the player is looking at? Apologies for the confusion.
     
  5. I am trying to limit placement of cupboard within a radius of another one, but i am not sure if i should use a raycast or simple check of cupboard position
     
  6. It sounds like you should be using something like Physics.OverlapSphere to check for all cupboards (BuildingPrivlidge) within some specified radius of where you are trying to place.
    Code:
    // check for entities within sphere with radius at position
    var hits = Physics.OverlapSphere(position, radius);
    // loop through hits and check for BuildingPrivlidge objects, check ownership, calculate distances
    foreach(var ent in hits)
    {
        BuildingPrivlidge privs = ent.GetComponentInParent<BuildingPrivlidge>();
        if(privs != null)
            // check ownership and distance here and handle appropriately
    }
    
     
    Last edited by a moderator: Jan 24, 2017