Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

using System.Collections; using System.Collections.Generic; using UnityEngine; ///…

  1. User

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    /// /// This is an enum of the various possible weapon types. /// It also includes a "shield" type to allow a shield PowerUp. ///

    public enum eWeaponType { none, //The default / no weapon blaster, //A simple blaster spread, //Multiple shots simultaneously phaser, //Shots that move in waves missile, //Homing missiles laser, //Damage over time shield //Raise shieldLevel }

    /// /// The WeaponDefinition class allows you to set the properties of a specific weapon in the Inspector /// The main class has an array of WeaponDefinitions that makes this possible ///

    [System.Serializable] public class WeaponDefinition { public eWeaponType type = eWeaponType.none; [Tooltip("Letter to show on the PowerUp Cube")] public string letter; [Tooltip("Color of PowerUp Cube")] public Color powerUpColor = Color.white; [Tooltip("Prefab of Weapon model that is attached to the Player Ship")] public GameObject weaponModelPrefab; [Tooltip("Prefab of projectile that is fired")] public GameObject projectilePrefab; [Tooltip("Color of the Projectile that is fired")] public Color projectileColor = Color.white; [Tooltip("Damage caused when a single Projectile hits an Enemy")] public float damageOnHit = 0; [Tooltip("Damage caused per second by the Laser [Not Implemented]")] public float damagePerSec = 0; [Tooltip("Seconds to delay between shots")] public float delayBetweenShots = 0; [Tooltip("Velocity of individual Projectiles")] public float velocity = 50; } public class Weapon : MonoBehaviour { static public Transform PROJECTILE_ANCHOR;

    [Header("Dynamic")]
    [SerializeField]
    [Tooltip("Setting this manually while playing does not work properly.")]
    private eWeaponType _type = eWeaponType.none;
    public WeaponDefinition def;
    public float nextShotTime; //Time the Weapon will fire next
    
    private GameObject weaponModel;
    private Transform shotPointTrans;
    
    private void Start()
    {
        //Set up PROJECTILE_ANCHOR if it has not already been done
        if (PROJECTILE_ANCHOR == null)
        {
            GameObject go = new GameObject("ProjectileAnchor");
            PROJECTILE_ANCHOR = go.transform;
        }
    
        shotPointTrans = transform.GetChild(0);
    
        //Call SetType() for the default_type set in the Inspector
        SetType(_type);
    
        //Find the fireEvent of a Hero Component in the parent hierarchy
        Hero hero = GetComponentInParent<Hero>();
        if (hero != null) hero.fireEvent += Fire;
    }
    
    public eWeaponType type
    {
        get
        {
            return (_type);
        }
        set
        {
            SetType(value);
        }
    }
    
    public void SetType(eWeaponType wt)
    {
        _type = wt;
        if (type == eWeaponType.none)
        {
            this.gameObject.SetActive(false);
            return;
        }
        else
        {
            this.gameObject.SetActive(true);
        }
        //Get the WeaponDefinition for this type from Main
        def = Main.GET_WEAPON_DEFINITION(_type);
        //Destroy any old model and then attach a model for this weapon
        if (weaponModel != null) Destroy(weaponModel);
        weaponModel = Instantiate<GameObject>(def.weaponModelPrefab, transform);
        weaponModel.transform.localPosition = Vector3.zero;
        weaponModel.transform.localScale = Vector3.one;
    
        nextShotTime = 0; //You can fire immediately after _type is set
    }
    
    private void Fire()
    {
        //If this.gameObject is inactive, return
        if (!gameObject.activeInHierarchy) return;
        //If it hasn't been enough time between shots, return
        if (Time.time < nextShotTime) return;
    
        ProjectileHero p;
        Vector3 vel = Vector3.up * def.velocity;
    
        switch (type)
        {
            case eWeaponType.blaster:
                p = MakeProjectile();
                p.vel = vel;
                break;
    
            case eWeaponType.spread:
                p = MakeProjectile();
                p.vel = vel;
                p = MakeProjectile();
                p.transform.rotation = Quaternion.AngleAxis(10, Vector3.back);
                p.vel = p.transform.rotation * vel;
                p = MakeProjectile();
                p.transform.rotation = Quaternion.AngleAxis(-10, Vector3.back);
                p.vel = p.transform.rotation * vel;
                break;
        }
    }
    
    private ProjectileHero MakeProjectile()
    {
        GameObject go;
        go = Instantiate<GameObject>(def.projectilePrefab, PROJECTILE_ANCHOR);
        ProjectileHero p = go.GetComponent<ProjectileHero>();
    
        Vector3 pos = shotPointTrans.position;
        pos.z = 0;
        p.transform.position = pos;
    
        p.type = type;
        nextShotTime = Time.time + def.delayBetweenShots;
        return (p);
    }
    

    }

  2. Assistant

    This script contains an enum called eWeaponType that represents the different possible weapon types in the game. It includes a "none" type for no weapon and a "shield" type for a shield power-up.

    There is also a WeaponDefinition class that allows you to set the properties of a specific weapon in the Inspector. It includes properties such as the letter to show on the power-up cube, the color of the power-up cube, the weapon model prefab, the projectile prefab, the color of the projectile, the damage caused on hit, the damage per second of a laser, the delay between shots, and the velocity of the projectiles.

    The Weapon class is responsible for firing projectiles based on the selected weapon type. It has a static variable called PROJECTILE_ANCHOR which is used to store the transform of a game object that will be used as the parent for all spawned projectiles.

    In the Start() method, the script sets up the PROJECTILE_ANCHOR if it hasn't already been done. It also finds the Hero component in the parent hierarchy and subscribes a Fire() method to its fireEvent event, so that the weapon can be fired when the hero fires.

    The type property allows you to get or set the weapon type. When the type is set, the script sets the corresponding WeaponDefinition from the Main class, creates or destroys the weapon model prefab based on the new type, and resets the nextShotTime variable.

    The Fire() method is called when the weapon should be fired. It checks if the weapon is active and if enough time has passed since the last shot. Then, depending on the weapon type, it creates one or multiple projectiles and sets their velocity accordingly.

    The MakeProjectile() method is responsible for creating a projectile game object, setting its position and type, and updating the nextShotTime.

    Overall, this script allows for different weapon types and their properties to be defined in the Inspector, and provides a way to shoot projectiles based on the selected weapon type.

  3. User

    How do I add a new weapon which utilizes 2 additional projectiles and a sine wave pattern similar to ( public override void Move() { //Because pos is a property, you can't directly set pos.x // so get the pos as an editable Vector3 Vector3 tempPos = pos; //theta adjusts based on time float age = Time.time - birthTime; float theta = Mathf.PI * 2 * age / waveFrequency; float sin = Mathf.Sin(theta); tempPos.x = x0 + waveWidth * sin; pos = tempPos;

        //Rotate a bit about y
        Vector3 rot = new Vector3(0, sin * waveRotY, 0);
        this.transform.rotation = Quaternion.Euler(rot);
    
        //base.Move() still handles the movement down in y
        base.Move();
    
        //print (bndCheck.isOnScreen);
    } )
    

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    /// /// This is an enum of the various possible weapon types. /// It also includes a "shield" type to allow a shield PowerUp. ///

    public enum eWeaponType { none, //The default / no weapon blaster, //A simple blaster spread, //Multiple shots simultaneously phaser, //Shots that move in waves missile, //Homing missiles laser, //Damage over time shield //Raise shieldLevel }

    /// /// The WeaponDefinition class allows you to set the properties of a specific weapon in the Inspector /// The main class has an array of WeaponDefinitions that makes this possible ///

    [System.Serializable] public class WeaponDefinition { public eWeaponType type = eWeaponType.none; [Tooltip("Letter to show on the PowerUp Cube")] public string letter; [Tooltip("Color of PowerUp Cube")] public Color powerUpColor = Color.white; [Tooltip("Prefab of Weapon model that is attached to the Player Ship")] public GameObject weaponModelPrefab; [Tooltip("Prefab of projectile that is fired")] public GameObject projectilePrefab; [Tooltip("Color of the Projectile that is fired")] public Color projectileColor = Color.white; [Tooltip("Damage caused when a single Projectile hits an Enemy")] public float damageOnHit = 0; [Tooltip("Damage caused per second by the Laser [Not Implemented]")] public float damagePerSec = 0; [Tooltip("Seconds to delay between shots")] public float delayBetweenShots = 0; [Tooltip("Velocity of individual Projectiles")] public float velocity = 50; } public class Weapon : MonoBehaviour { static public Transform PROJECTILE_ANCHOR;

    [Header("Dynamic")]
    [SerializeField]
    [Tooltip("Setting this manually while playing does not work properly.")]
    private eWeaponType _type = eWeaponType.none;
    public WeaponDefinition def;
    public float nextShotTime; //Time the Weapon will fire next
    
    private GameObject weaponModel;
    private Transform shotPointTrans;
    
    private void Start()
    {
        //Set up PROJECTILE_ANCHOR if it has not already been done
        if (PROJECTILE_ANCHOR == null)
        {
            GameObject go = new GameObject("ProjectileAnchor");
            PROJECTILE_ANCHOR = go.transform;
        }
    
        shotPointTrans = transform.GetChild(0);
    
        //Call SetType() for the default_type set in the Inspector
        SetType(_type);
    
        //Find the fireEvent of a Hero Component in the parent hierarchy
        Hero hero = GetComponentInParent<Hero>();
        if (hero != null) hero.fireEvent += Fire;
    }
    
    public eWeaponType type
    {
        get
        {
            return (_type);
        }
        set
        {
            SetType(value);
        }
    }
    
    public void SetType(eWeaponType wt)
    {
        _type = wt;
        if (type == eWeaponType.none)
        {
            this.gameObject.SetActive(false);
            return;
        }
        else
        {
            this.gameObject.SetActive(true);
        }
        //Get the WeaponDefinition for this type from Main
        def = Main.GET_WEAPON_DEFINITION(_type);
        //Destroy any old model and then attach a model for this weapon
        if (weaponModel != null) Destroy(weaponModel);
        weaponModel = Instantiate<GameObject>(def.weaponModelPrefab, transform);
        weaponModel.transform.localPosition = Vector3.zero;
        weaponModel.transform.localScale = Vector3.one;
    
        nextShotTime = 0; //You can fire immediately after _type is set
    }
    
    private void Fire()
    {
        //If this.gameObject is inactive, return
        if (!gameObject.activeInHierarchy) return;
        //If it hasn't been enough time between shots, return
        if (Time.time < nextShotTime) return;
    
        ProjectileHero p;
        Vector3 vel = Vector3.up * def.velocity;
    
        switch (type)
        {
            case eWeaponType.blaster:
                p = MakeProjectile();
                p.vel = vel;
                break;
    
            case eWeaponType.spread:
                p = MakeProjectile();
                p.vel = vel;
                p = MakeProjectile();
                p.transform.rotation = Quaternion.AngleAxis(10, Vector3.back);
                p.vel = p.transform.rotation * vel;
                p = MakeProjectile();
                p.transform.rotation = Quaternion.AngleAxis(-10, Vector3.back);
                p.vel = p.transform.rotation * vel;
                break;
        }
    }
    
    private ProjectileHero MakeProjectile()
    {
        GameObject go;
        go = Instantiate<GameObject>(def.projectilePrefab, PROJECTILE_ANCHOR);
        ProjectileHero p = go.GetComponent<ProjectileHero>();
    
        Vector3 pos = shotPointTrans.position;
        pos.z = 0;
        p.transform.position = pos;
    
        p.type = type;
        nextShotTime = Time.time + def.delayBetweenShots;
        return (p);
    }
    

    }

  4. Assistant

    To add a new weapon that utilizes 2 additional projectiles and a sine wave pattern, you can follow these steps:

    1. Add a new weapon type to the eWeaponType enum.
    2. Create a new WeaponDefinition in the Inspector with the properties for the new weapon.
    3. Inside the Fire() method of the Weapon class, add a case for the new weapon type.
    4. In the case for the new weapon type, create 2 additional projectiles using the MakeProjectile() method, and set their velocities and rotations to create the desired spread pattern. You can use the existing code for the spread weapon type as a reference.
    5. Implement the new projectile behavior by modifying the MakeProjectile() method or creating a new method specifically for the new projectile behavior. In this method, you can set the projectile's movement pattern to a sine wave by adjusting the position and rotation over time. You can use the Move() method from the ProjectileHero class that you mentioned in your question.

    Here's an example of how the modified code could look like:

    // ...
    
    public enum eWeaponType
    {
        none,
        blaster,
        spread,
        phaser,
        missile,
        laser,
        shield,
        newWeapon // New weapon type
    }
    
    public class Weapon : MonoBehaviour
    {
        // ...
    
        private void Fire()
        {
            // ...
    
            switch (type)
            {
                // ...
    
                case eWeaponType.newWeapon:
                    ProjectileHero p1 = MakeProjectile();
                    ProjectileHero p2 = MakeProjectile();
                    ProjectileHero p3 = MakeProjectile();
    
                    p1.vel = vel;
                    p2.transform.rotation = Quaternion.AngleAxis(10, Vector3.back);
                    p2.vel = p2.transform.rotation * vel;
                    p3.transform.rotation = Quaternion.AngleAxis(-10, Vector3.back);
                    p3.vel = p3.transform.rotation * vel;
    
                    // Set additional projectile properties here
    
                    p1.Move(); // Call Move() method to start the sine wave pattern
                    break;
    
                // ...
            }
        }
    
        // ...
    }
    

    Note that you may need to modify the ProjectileHero class to include the Move() method with the sine wave pattern you mentioned. This method would be responsible for adjusting the position and rotation of the projectile over time to create the desired wave pattern.