Autoaiming with tags.

Sup y’all!

So far, what I’ve been making is a 2D, 1v1, topdown shooter.

I searched for autoaiming scripts, and so far the one that worked the best has been this one:

    private void GetRotation()
    {
        Vector2 direction = RotationTarget.position - transform.position;

        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, RotationSpeed * Time.deltaTime);
    }

My question: Is there any way for the script to aim automatically towards game objects with a certain tag, instead of by transform? Everything I´ve found is just the tutorial that teaches this, and input related ones (Joystick, keyboard, mouse, etc.).

There’s no need for it to aim to closest, or for a distance cap on the aim, given the mechanics I plan for my game to have, just that it aims to any game object with a certain tag (Given that the game object/player character may change, but tags will be the same across the board).

Any help is appreciated, thanks!

All you’d need to do is reference the Transform of the gameobject you find by tag.
For example:

RotationTarget = GameObject.FindGameObjectWithTag("Tag").transform;

It sounds like you need to restructure your code a little as a tag is just a way to FIND an object… once you have the object you should store a proper reference to either it’s script (ie: Player.cs, Enemy1.cs etc) or it’s Transform component.

1 Like

I already had the variable declarated, I just didn’t show it (Sorry, slipped my mind and it was 9am, not much brain processing power then lol).

In the end this is how my code turned out:

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

public class PxRotation1 : MonoBehaviour
{
    [SerializeField]private float rotationSpeed = 15f;   
    public GameObject rotationTarget;                   

    void Start()
    {
        GetRotationTarget();
    }

    void Update()
    {
        GetRotation();
    }

    private void GetRotationTarget()
    {
        rotationTarget = GameObject.FindWithTag("Player");
    }

    private void GetRotation()
    {
        Vector2 direction = rotationTarget.transform.position - transform.position;

        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
    }

}

And it works great!
Now I just have to find out if there’s a way to make the script not autoaim towards itself (Or better said, the gameObject its bound to) and it will be complete and fully functional for what I want it. Small steps, and all that. (Any help with this would also be appreciated).
Anyways, sorry for the late response, and thanks! This helped me get on the right track lol.

You want
GameObject.FindGameObjectsWithTag
This returns an array, which should have 2 elements in your case. Loop through them and see which one isn’t the one with the script, which you can identify using the “this” keyword. So something like the following (not tested). (Also I’m sure there are other and probably better ways…)

public GameObject rotationTarget;
    public GameObject[] taggedObjects;
    void Start()
    {
            taggedObjects = GameObject.FindGameObjectsWithTag("Player");

        foreach (GameObject tagged in taggedObjects )
        {
            if(tagged != this.gameObject) {
                    rotationTarget = tagged;
            }
        }
    }
1 Like

Wow, it really works! Tested it, and there are no problems whatsoever.
Also, don’t worry on the “better ways” thing, this is exactly what I was looking for.
Thank you very much!

1 Like

Great, glad it helped!
One note, this technique would only work with 2 objects, one “this” object and the other one. If there were more “not-this” objects, you’d have to see which one was closest (or use some other criteria) to determine which to aim at, because it will loop through all of them and keep setting rotationTarget to each one that’s “not-this”. So the eventual one set to rotationTarget would be the last in the array, which could be any of them.

1 Like

Yep, and that’s all I need for this specific project. It’s a 1v1 shooter, with no extras/other enemies/distractions, so more code would just be overcomplicating things. Once again, thank you very much! :slight_smile:

1 Like