コード例 #1
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var go = new GameObject("Grenade Out!");
            go.transform.position = transform.position + transform.right * 0.5f + transform.up * 0.5f;

            var visible = Instantiate <GameObject>(ProjectilePrefab, go.transform);
            foreach (var collider in new List <Collider>(visible.GetComponentsInChildren <Collider>()))
            {
                Destroy(collider);
            }

            // initial toss motion
            Vector3 tossVelocity = transform.forward * RelativeMotion.z + transform.up * RelativeMotion.y;
            // maybe add in user motion
            if (GetTosserMotion != null)
            {
                tossVelocity += GetTosserMotion();
            }
            // parabolic motion
            Ballistic3D.Attach(go, tossVelocity, GravityForThisWeapon);

            // this gets called when the RaycastSensor notices you hit something
            System.Action <Vector3> OnHit = (pos) => {
                Instantiate <GameObject>(ExplosionPrefab, pos, Quaternion.identity);

                damager.ApplyDamage(pos, DamageConfig);

                Destroy(go);
            };
            RaycastSensor.Attach(go, OnHit);

            // death sentence
            TTL.Attach(go, 10.0f);

            // make it tumble
            var spinner = go.AddComponent <SpinMeAllAxes>();
            spinner.RateOfSpin = Random.onUnitSphere * Random.Range(1000, 2000);
        }
    }
コード例 #2
0
 // Start is called before the first frame update
 void Start()
 {
     _robot         = transform.GetComponent <RobotMover>();
     _sensorForward = _robot._sensorForward;
 }
コード例 #3
0
    public void InitializeModules(AgentGenome genome, Agent agent)
    {
        atmosphereSensorList = new List <AtmosphereSensor>();
        basicJointList       = new List <BasicJoint>();
        basicWheelList       = new List <BasicWheel>();
        contactSensorList    = new List <ContactSensor>();
        gravitySensorList    = new List <GravitySensor>();
        healthModuleList     = new List <HealthModule>();
        oscillatorList       = new List <InputOscillator>();
        raycastSensorList    = new List <RaycastSensor>();
        shieldList           = new List <Shield>();
        targetSensorList     = new List <TargetSensor>();
        thrusterEffectorList = new List <ThrusterEffector>();
        torqueEffectorList   = new List <TorqueEffector>();
        trajectorySensorList = new List <TrajectorySensor>();
        valueList            = new List <InputValue>();
        weaponProjectileList = new List <WeaponProjectile>();
        weaponTazerList      = new List <WeaponTazer>();

        for (int i = 0; i < genome.bodyGenome.atmosphereSensorList.Count; i++)
        {
            AtmosphereSensor atmosphereSensor = new AtmosphereSensor();
            atmosphereSensor.Initialize(genome.bodyGenome.atmosphereSensorList[i], agent);
            atmosphereSensorList.Add(atmosphereSensor);
        }
        for (int i = 0; i < genome.bodyGenome.basicJointList.Count; i++)
        {
            BasicJoint basicJoint = new BasicJoint();
            basicJoint.Initialize(genome.bodyGenome.basicJointList[i], agent);
            basicJointList.Add(basicJoint);
            //basicJointList[i].Initialize(genome.basicJointList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.basicWheelList.Count; i++)
        {
            BasicWheel basicWheel = new BasicWheel();
            basicWheel.Initialize(genome.bodyGenome.basicWheelList[i], agent);
            basicWheelList.Add(basicWheel);
            //OLD:
            //basicWheelList[i].Initialize(genome.basicWheelList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.contactSensorList.Count; i++)
        {
            ContactSensor contactSensor = new ContactSensor();
            //agent.segmentList[genome.contactSensorList[i].parentID].AddComponent<ContactSensorComponent>();
            contactSensor.Initialize(genome.bodyGenome.contactSensorList[i], agent);
            contactSensorList.Add(contactSensor);

            //contactSensorList[i].Initialize(genome.contactSensorList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.gravitySensorList.Count; i++)
        {
            GravitySensor gravitySensor = new GravitySensor();
            gravitySensor.Initialize(genome.bodyGenome.gravitySensorList[i], agent);
            gravitySensorList.Add(gravitySensor);
        }
        for (int i = 0; i < genome.bodyGenome.healthModuleList.Count; i++)
        {
            HealthModule healthModule = new HealthModule();
            //agent.segmentList[genome.healthModuleList[i].parentID].AddComponent<HealthModuleComponent>();
            healthModule.Initialize(genome.bodyGenome.healthModuleList[i], agent);
            healthModuleList.Add(healthModule);
            //healthModuleList[i].Initialize(genome.healthModuleList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.oscillatorInputList.Count; i++)
        {
            InputOscillator inputOscillator = new InputOscillator();
            inputOscillator.Initialize(genome.bodyGenome.oscillatorInputList[i], agent);
            oscillatorList.Add(inputOscillator);
            //oscillatorList[i].Initialize(genome.oscillatorInputList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.raycastSensorList.Count; i++)
        {
            RaycastSensor raycastSensor = new RaycastSensor();
            raycastSensor.Initialize(genome.bodyGenome.raycastSensorList[i], agent);
            raycastSensorList.Add(raycastSensor);
            //raycastSensorList[i].Initialize(genome.raycastSensorList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.shieldList.Count; i++)
        {
            Shield shield = new Shield();
            shield.Initialize(genome.bodyGenome.shieldList[i], agent);
            shieldList.Add(shield);
        }
        for (int i = 0; i < genome.bodyGenome.targetSensorList.Count; i++)
        {
            TargetSensor targetSensor = new TargetSensor();
            targetSensor.Initialize(genome.bodyGenome.targetSensorList[i], agent);
            targetSensorList.Add(targetSensor);
            //targetSensorList[i].Initialize(genome.targetSensorList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.thrusterList.Count; i++)
        {
            // Create Functional GameObjects & Components:
            // none in this case

            // Create Logic Module:
            ThrusterEffector thrusterEffector = new ThrusterEffector();
            // Initialize and HookUp Logic Module:
            thrusterEffector.Initialize(genome.bodyGenome.thrusterList[i], agent);
            // If Visible, Create Renderable GameObjects & Components:
            if (isVisible)
            {
                // Find appropriate Prefab based on Agent & Module Genome:
                GameObject thrusterGO = Instantiate(Resources.Load("Prefabs/Modules/Thrusters/thrusterTest")) as GameObject;
                thrusterGO.transform.parent        = agent.segmentList[genome.bodyGenome.thrusterList[i].parentID].transform;
                thrusterGO.transform.localPosition = genome.bodyGenome.thrusterList[i].forcePoint;
                thrusterGO.transform.localRotation = Quaternion.identity;
                // Hook into Logic Module
                thrusterEffector.thrusterComponent = thrusterGO.GetComponent <ThrusterComponent>();
            }
            // Add Logic Module to Agent's Master List
            thrusterEffectorList.Add(thrusterEffector);
        }
        for (int i = 0; i < genome.bodyGenome.torqueList.Count; i++)
        {
            TorqueEffector torqueEffector = new TorqueEffector();
            torqueEffector.Initialize(genome.bodyGenome.torqueList[i], agent);
            torqueEffectorList.Add(torqueEffector);
            //torqueEffectorList[i].Initialize(genome.torqueList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.trajectorySensorList.Count; i++)
        {
            TrajectorySensor trajectorySensor = new TrajectorySensor();
            trajectorySensor.Initialize(genome.bodyGenome.trajectorySensorList[i], agent);
            trajectorySensorList.Add(trajectorySensor);
        }
        for (int i = 0; i < genome.bodyGenome.valueInputList.Count; i++)
        {
            InputValue inputValue = new InputValue();
            inputValue.Initialize(genome.bodyGenome.valueInputList[i], agent);
            valueList.Add(inputValue);
            //valueList[i].Initialize(genome.valueInputList[i]);
        }
        for (int i = 0; i < genome.bodyGenome.weaponProjectileList.Count; i++)
        {
            WeaponProjectile weaponProjectile = new WeaponProjectile();
            weaponProjectile.Initialize(genome.bodyGenome.weaponProjectileList[i], agent);

            if (isVisible)
            {
                // Find appropriate Prefab based on Agent & Module Genome:
                GameObject weaponGO = Instantiate(Resources.Load("Prefabs/Modules/WeaponProjectiles/projectileTest")) as GameObject;
                weaponGO.transform.parent        = agent.segmentList[genome.bodyGenome.weaponProjectileList[i].parentID].transform;
                weaponGO.transform.localPosition = genome.bodyGenome.weaponProjectileList[i].muzzleLocation;
                weaponGO.transform.localRotation = Quaternion.identity;
                // Hook into Logic Module
                weaponProjectile.weaponProjectileComponent = weaponGO.GetComponent <WeaponProjectileComponent>();
            }

            weaponProjectileList.Add(weaponProjectile);

            /*weaponProjectileList[i].Initialize(genome.weaponProjectileList[i]);
             *
             * if (isVisible) {
             *  GameObject particleGO = Instantiate(Resources.Load(weaponProjectileList[i].GetParticleSystemURL())) as GameObject;
             *  ParticleSystem particle = particleGO.GetComponent<ParticleSystem>();
             *  ParticleSystem.EmissionModule emission = particle.emission;
             *  emission.enabled = false;
             *  particle.gameObject.transform.parent = rootObject.transform;
             *  particle.gameObject.transform.localPosition = new Vector3(0f, 0.5f, 0f);
             *  particle.gameObject.transform.localRotation = Quaternion.identity;
             *  weaponProjectileList[i].particles = particle; // save reference
             * }
             */
        }
        for (int i = 0; i < genome.bodyGenome.weaponTazerList.Count; i++)
        {
            WeaponTazer weaponTazer = new WeaponTazer();
            weaponTazer.Initialize(genome.bodyGenome.weaponTazerList[i], agent);

            if (isVisible)
            {
                // Find appropriate Prefab based on Agent & Module Genome:
                GameObject weaponGO = Instantiate(Resources.Load("Prefabs/Modules/WeaponTazers/tazerTest")) as GameObject;
                weaponGO.transform.parent        = agent.segmentList[genome.bodyGenome.weaponTazerList[i].parentID].transform;
                weaponGO.transform.localPosition = genome.bodyGenome.weaponTazerList[i].muzzleLocation;
                weaponGO.transform.localRotation = Quaternion.identity;
                // Hook into Logic Module
                weaponTazer.weaponTazerComponent = weaponGO.GetComponent <WeaponTazerComponent>();
            }

            weaponTazerList.Add(weaponTazer);

            /*weaponTazerList[i].Initialize(genome.weaponTazerList[i]);
             * if (isVisible) {
             *  GameObject particleGO = Instantiate(Resources.Load(weaponTazerList[i].GetParticleSystemURL())) as GameObject;
             *  ParticleSystem particle = particleGO.GetComponent<ParticleSystem>();
             *  ParticleSystem.EmissionModule emission = particle.emission;
             *  emission.enabled = false;
             *  particle.gameObject.transform.parent = weaponTazerList[i].parentBody.transform;
             *  particle.gameObject.transform.localPosition = weaponTazerList[i].muzzleLocation;
             *  particle.gameObject.transform.localRotation = Quaternion.identity;
             *  weaponTazerList[i].particles = particle; // save reference
             * }
             */
        }
    }