rotate turret toward target unity

Hello everyone,
I am trying to rotate turret toward target. I tried different approach, but it’s rotating additional 90 degree in y axis.

Here are scripts I tried:
First Method:

transform.LookAt(FindClosestEnemy().transform);

Second Method:

transform.rotation = Quaternion.Slerp(transform.rotation,
            Quaternion.LookRotation(FindClosestEnemy().transform.position), 10 * Time.deltaTime);

I also tried RotateToward, but same result.

Script to find target:

public GameObject FindClosestEnemy()
    {
       
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("AI");
       
        GameObject closest = null;
       
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }
        return closest;
    }

What can be the reason of additional 90 degree rotation?
Thanks

I think maybe using Quaternion.Lerp might work, haven’t really tried but I think that might be the solution

1 Like

Thanks, but maybe problem is something different. As above methods working correctly in my other game.

Well then, as far as I know the only other problem could be that the turret can’t find the closest enemy. Give me a day to think I’m tired right now lol

Turret is finding enemy correctly. I tried this, and it’s returning right object name:

Debug.Log(FindClosestEnemy().name);

Oh, if everything else is working fine, why not this? Do you have a rigidbody on the turret?

no rigidbody.

Oh, I thought you might’ve frozen the rotation constraints. Okay man this is too confusing for me, you’ll have to wait for the experts:)

EDIT: Sorry I couldn’t be any help

This method ^ ^ ^ ^ optionally takes a second argument that is your UP vector for the purposes of looking. This defaults to Vector3.up. If that is not UP in your setup, you will get incorrect results.

Alternatively, here is my turret aiming code… it has traverse but also includes elevation, like a battleship gun, but you could just use it for the traverse and disregard the elevation portions.

Turret aiming/rotating:

https://discussions.unity.com/t/783469/2

Lines 43 and 44 above calculate desired traverse.

Lines 64 and 67 gradually slew the traverse to the target.

General aiming / vector differences / deltas:

https://discussions.unity.com/t/803625/2

1 Like

Thank you so much for help. But as Player Car is moving and Target Car is also moving, Maybe that’s why turret is always targeting little behind of target(No bullet hitting target). This is my code now:

Vector3 DeltaToTarget;
float DesiredTraverse;
public float rotationSpeed = 50;

    void FixedUpdate()
    {
        DeltaToTarget = FindClosestEnemy().position - transform.position;
        DesiredTraverse = Mathf.Atan2( DeltaToTarget.x, DeltaToTarget.z) * Mathf.Rad2Deg;

        Quaternion trav = Quaternion.Euler( 0, DesiredTraverse, 0);
        transform.localRotation = Quaternion.Lerp( transform.localRotation, trav, rotationSpeed * Time.deltaTime);
    }

7212292--866803--Capture3.jpg

That would do it!

Now you need a predictor for the target.

The simplest target predictor estimates time (based on distance and bullet flight) to impact, then extrapolates the target’s relative velocity and uses that new point as its aimpoint.

This works best if the target is moving 90 degrees to your bullet path. Any other angle will have error.

Error can be accommodated by:

  • final steer (cheating) with the bullet
  • having the target / bullet hit radius larger so it still hits

A more advanced predictor observes each shot and adjusts it.

ALL of these predictors presume relative target velocity remains constant.

Tower defense predictors usually cheat by choosing in advance where the bullet will connect, then tweening the bullet there as a function of the target’s distance from where it was when the bullet fired to where it was at intended impact, and faking it.

1 Like