Why the gameobject blue axis is facing forward but the gun is on the red axis and not the blue?

Viewed 71

I tried to create a empty gameobject and put the first gameobject as child but it didn't fix the problem. Just rotating the object a script is fine but if I'm using LookAt it's not rotating good since the gun is pointing on the red axis and the blue axis is on the body.

Wrong axis

I tried simple LookAt :

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

public class LookAT : MonoBehaviour 
{
    private GameObject target;

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

Then I tried this : This was a bit better it seems like it was facing with the red axis forward with the gun but not perfect still it didn't target perfect the target I think not sure and not sure if this is a good solution :

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

public class LookAT : MonoBehaviour
{
    public float speed;

    private GameObject target;

    // Use this for initialization
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Enemy");
    }
    // Update is called once per frame
    void Update()
    {
        Vector3 direction = target.transform.position - transform.position;
        Quaternion toRotation = Quaternion.FromToRotation(transform.right, direction);
        transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
    }
}

The second solution I tried seems the red axis with the gun is facing the target now but the gun and the rings on the base are stuttering and the rings rotating too but they should not rotate I think or they should not be part of the looking on the target.

In this screenshot the whole rings on the base are kind of looking at the target and make the whole turret looks strange. So I'm not sure how ot make this rings and other stuff rotating while only the turret turn object will face the target.

Wrong stuff is rotating and looking facing the target

1 Answers

Looks like the model has axis that don't line up with how you want to use it. Its reccomended that you fix the model, but alternatively you can also add a parent transform on top so you can add some rotation to your model.

Check this out for a more detailed explanation.

To get the two discrete axis of rotation thing I think you are describing at the end of your post you can add two parents as described in the post I linked and apply rotations to each of those parents individually.

Related