Solved Set cargoplane velocity

Discussion in 'Rust Development' started by DroneFW, Jun 1, 2015.

  1. Hi, does someone know how to set the velocity on the airdrop planes? So far I've tried this:
    Code:
            void OnEntitySpawned(BaseNetworkable entity)
            {
                    CargoPlane plane = entity.GetComponent<CargoPlane>();
                    if (plane == null)
                    {
                        return;
                    }                plane.SetVelocity(new UnityEngine.Vector3(0, 0, 3000));
                 }
              }
    Obviously I've tried different values for the vector but didn't seem to work.
     
  2. You normally just have to modify the secondsToTake field on the CargoPlane object.
    Code:
    using System;
    using UnityEngine;public class CargoPlane : BaseEntity
    {
        private bool dropped;
        private Vector3 dropPosition = Vector3.zero;
        private Vector3 endPos;
        public SpawnFilter filter;
        public string prefabToDrop = "items/supply_drop";
        private float secondsTaken;
        private float secondsToTake;
        private Vector3 startPos;    private void Update()
        {
            if (base.isServer)
            {
                this.secondsTaken += Time.deltaTime;
                float t = Mathf.InverseLerp(0f, this.secondsToTake, this.secondsTaken);
                if (!this.dropped && (t >= 0.5f))
                {
                    this.dropped = true;
                    BaseEntity entity = GameManager.server.CreateEntity(this.prefabToDrop, base.transform.position, new Quaternion(), true);
                    if (entity != null)
                    {
                        entity.globalBroadcast = true;
                        entity.Spawn(true);
                    }
                }
                base.transform.position = Vector3.Lerp(this.startPos, this.endPos, t);
                base.TransformChanged();
                if (t >= 1f)
                {
                    base.SendMessage("KillMessage", SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
     
  3. Thanks for the reply, i'll give it a try. May I ask where you get this kind of code from? I'm kinda new to writing rust plugins and mostly find examples in other people's code. Is there any way to find all this stuff out by yourself? Decompiling the Assembly-CSharp.dll doesn't supply that much info.
     
  4. That code is from decompiling the Assembly-CSharp.dll actually :)
    upload_2015-6-2_0-13-31.png
     
  5. How would I set the secondsToTake field, as it's private?
     
  6. You should be able to set the value with Reflection.

    Something like this (untested):
    Code:
    ...
            FieldInfo secondsToTake = typeof(CargoPlane).GetField("secondsToTake", BindingFlags.NonPublic | BindingFlags.Instance);
            ...
            void OnEntitySpawned(BaseNetworkable entity)
            {
                    var plane = entity as CargoPlane;
                    if (plane == null) return;
                    var secsToTake = secondsToTake.GetValue(plane);
                    Puts($"It will take the plane {secsToTake}s");
                    secondsToTake.SetValue(plane, 100f);
             }
    ...
     
  7. I just tested this and this amazingly works! I'm reading stuff about Reflection and I'm amazed with the possibilities this will give. Thank's a lot for this tip, I guess it will be of great value in the future!