Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotating all objects with tag X in an array towards target/cursor. Is this a good approach?

Discussion in 'Getting Started' started by Graephen, Jul 25, 2023.

  1. Graephen

    Graephen

    Joined:
    Apr 18, 2021
    Posts:
    25
    I'm making a 2D space game, which I aim to constantly pile-into as I learn things, because it's the least complicated type of a game asset-vise.
    My ships will have hardpoints, which the player can fit weapons onto. These will be two types; guns and turrets. Guns I aim to clamp their rotation and put the hardpoints in the front of the ships, while turrets move 360° and are littered all over a ship, which is why they are a separate variable.

    Does this make sense, by the way? Maybe I could just clamp the guns separately and group turrets and guns under one array, but I'm not sure if there woudln't be any problems, since one type is clamped while the other isn't. Anyway!

    I have a "ShipStats" script, which is attached to every spaceship. This one determines stuff like rotation speed, overall speed, health in the future, ammount of turret and weapon slots etc.
    I aim to have both the Player and the Enemies use this exact same script to determine all their stats and hardpoints etc. and they will both be accessed by individual scripts - one for player, one for the enemies.

    I'll create an array of GameObjects, whose amount determines how many hardpoints of what weapon the ship can mount and their location on the ship, which is determined by a child GameObject on the ship itself.
    That's the first screenshot attached.

    So basically, all I need this first part of the script to do, is count all the hardpoints on the ship and put them in an array. I think I could achieve this using

    public GameObject[] turretHardpoint;
    void Update ()
    {
    turretHardpoint = GameObject.FindGameObjectsWithTag("Turret Hardpoint");
    }

    Would this be the correct and performance-friendly approach?

    Then I need to instantiate a prefab of a turret on such a hardpoint, which is a child object of the hardpoint, such as in the first screenshot, depending on which the player equips. I'm far from learning about how to do that, because I need to make an equipment UI first, so I'll make a placeholder script instead, that equips one kind of turrets all over the ship on start.

    Then I need to turn the hardpoint, or the child - doesen't matter which, really, as long as it doesen't cause issues when I'll be doing the shooting mechanics. And I need to turn all of them at once towards the target.
    I have this script from a YouTube tutorial;


    Code (CSharp):
    1.     void TrackCursor()
    2.     {
    3.         Vector2 turretDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    4.         float angleTowardsCursor = Mathf.Atan2(turretDirection.y, turretDirection.x) * Mathf.Rad2Deg;
    5.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    6.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turretRotationSpeed * Time.fixedDeltaTime);
    ... but if I try to replace "transform" in all the instances with the array from another script i.e. shipStats.turretHardpoint.position I get

    Error CS1061 'GameObject[]' does not contain a definition for 'position' and no accessible extension method 'position' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp, Assembly-CSharp.Player

    So it's not the way to do it, obviously, but what is? How do I rotate an array of objects - all of them, on another object towards the mouse cursor, or towards one target?

    Thank you for any and all feedback. I'm going to watch a few hours of tutorials on arrays meanwhile, maybe I'll learn something useful in this case, but I'm relying on these forums for help, haha.
     

    Attached Files:

  2. Graephen

    Graephen

    Joined:
    Apr 18, 2021
    Posts:
    25
    I am slowly figuring things out, but I do still need help.
    So basically, I need the Vector2 of all the Objects in an Array and then I want to rotate all of them towards the mouse cursor/target. How'd I go about doing this?
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    All you seem to need is to read about how to access elements in an array. You can't use transform on an array. You need to loop through the elements inside the array, and use transform on those elements.

    Looking for objects with Find functions is better not used in the Update() function. Maybe you can find a way to use an action/event on turretHardpoint change, to let whoever needs to know, know about the change. Or, if it only needs to find that point at the start, put the command in Start() or Awake().
     
  4. Graephen

    Graephen

    Joined:
    Apr 18, 2021
    Posts:
    25
    Yeah, I know it's inefficient, but I'm too bad at coding to take-on events. Basically, the only time I need to find a change in how the hardpoints are set-up, is when leaving some sort of a base/spacestation/trader etc. with a new ship, or new equipment. That's how I plan on doing it, but so far, everything is a placeholder until I learn more.

    I don't even know what I'd use to do this efficiently.

    I'll check out how to access objects in an array and I'll try to put all my Find functions into start for now, see how that goes.
     
    ijmmai likes this.