Why when using LookAt to make the turret facing the target it's facing the other direction ?

The script have one line in the Update.
The script is attached to the Sphere.

The Sphere does rotate and look at the target in this case a Cube.
But the child of the Sphere Cube (1) is rotating and facing other direction and not facing to the target with the Sphere.

The blue axis of the Sphere is facing the target the Cube. When I move the Cube target to the right or left the Sphere blue axis keep facing it. But the Cube (1) isk eep facing the other direction it’s rotating with the Sphere but never facing the target.

And a screenshot example after moved the target Cube a bit:
The Sphere blue axis is keep facing the target Cube but the Cube (1) is facing another direction:

This is the script attached to the Sphere:

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

public class Rotateturret : MonoBehaviour
{
    public Transform target;

    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        transform.LookAt(target);
    }
}

I also have another model not my model of a turret the one in golden in the screenshots.
Turret 1a

If I attach the same script to the Turret 1a the result is the same as in my turret.
The Turret 1a blue axis is facing the target but the machine gun the Pylon or the Turret child of the Pylon are facing another direction:

If I attach the script only to one of the childs for example to the Turret then the same problem appears:

I can’t figure out why the child/s never facing also to the target only the parent seems to be facing the target.
I tried to change in my turret the Cube (1) position to 0,0,0 but it didn’t change much.

    public Transform target;
    Vector3 offset;

    void Start()
    {
        offset = transform.TransformDirection(target.position - transform.position);
    }

    void LateUpdate()
    {
        float angleY = target.transform.eulerAngles.y;

        Quaternion rotation = Quaternion.Euler(0, angleY, 0);
        transform.LookAt(target.transform);
    }

there is also a free turret script on the store. more complete.

It’s because your turret doesn’t have Unity’s coordinates. One simple fix is to parent it to an empty and then move the empty instead.

1 Like

Oups thaught you would also follow the target kinda drone sentinel… worst i forgot the position statement.
transform.position = target.position - (rotation * offset);

nvm fire7side is right. the cube into the sphere(child) must be resat to rotation (0,0,0).

1 Like

I found that the only thing that is working is if I change the Cube (1) position and rotation to be like the Sphere before running the game.

I tried alexeu and I tried to parent it to empty GameObject but it didn’t work.

And yet it’s working I can’t yet understand why it does and why it’s so hard to make it working without all this changes.
I just added new Sphere and Cube to my scene why is it so hard to rotate them both at the same time facing another GameObject ?

Anyway in the screenshot the Sphere Blue Z axis is pointing to the right I guess this is the Sphere face direction ?
Once I rotated the Cube (1) that the Blue Z axis will face same direction of the Sphere they are both rotating to the same angle/position:

And the Cube (1)

I understand the logic when running the game both sphere and cube should be facing same direction.
But now each time I combine two gameobjects I need to set them to be facing same direction to rotate them ? Isn’t it should be one object like a prefab ? Or if I put them as childs of empty GameObject but then it seems like the empty GameObject is facing wrong direction at the start so the whole rotation i wrong.

Anyway it’s working but I’m still confused. Why I need to set both Sphere and Cube (1) to be same direction of axis Z before running the game ? And I’m still rotating the empty GameObject but I had to set both Sphere and Cube (1) to face same Z axis direction first.

What are the unity coordinates ? Do you mean space/local coordinates ? I didn’t understand that part. When I’m adding a new 3d object to the scene like sphere quad cube in what coordinates are they if not Unity’s ?

Those are unity’s. Usually when a lookat doesn’t work it’s because the model isn’t aligned with the z axis. Other than that, it pretty much has to work if you are using transform.lookat. It’s just pointing the z axis of the model toward the target. You are using a sphere, so it’s hard to tell where the front of it is. If you look at your pictures, the blue arrow always points at the cube. That’s the front of the sphere. You can either put the barrel on the front of the sphere, or parent it to an empty with the barrel pointed at the z axis front. For me, it happens when I import a model and the only thing I can do is parent to an empty or realign it in the modeling program.

1 Like