- Home /
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.
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:
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.
Answer by FlaSh-G · Apr 04, 2018 at 12:08 PM
Check the right of your move/rotate/etc gizmos at the top left of the editor. It says "Center" and "Local". The "Local" button indicates that the three arrows you see on the object represent the local axes of the object. The blue arrow represents the Z axis, which in Unity's coordinate system is "forward". As you can see, your turret is actually facing the target object, but the mesh it renderes has the cannon facing to the right (the red arrow, representing the X axis).
To fix this, you could either update your mesh in a modelling program of your choice, or, as is preferred by most people i think, do the following:
Create an empty GameObject.
Make your turret a child of that GameObject.
Set position and rotation of the child so that the cannon is facing "forward" as seen from the new parent, meaning that your cannon is aligned with the blue arrow when you have the parent selected.
Put the script on the parent instead of the model itself.
I recorded a short video clip showing what I think is the problem. The small pink turret is no my turret I took it from another someone project and this small pink turret is working fine.
The big golden turret is not my one either but the big one is not rotating like the small one. When I move the target the Cube in the scene while the game is running the small turret keep tracking it on the red (X) and blue (Z) axis But the big turret is not tracking the target on this axis.
I can't see anything special about the small turret then the big one. Both rotations seems to be the same and aligned to each one to his gun.
Some days ago I created my own turret from a sphere and a cube the cube was the gun and child of the sphere and had the same problem like the big turret here in the video. I can't figure out why is it so hard to make a simple tracking objects. And why the small turret in the video is working fine but the big one is not.
Please look at the video and I can send also my project maybe something is wrong but it's a new clean project.
Can you explain to me according to the video what is wrong ? And why I need to make so many changes while the small turret seems to be so simple and working so fine ? I don't understand it yet.
https://www.youtube.com/watch?v=-0LWQVpCpuE&feature=youtu.be