1
\$\begingroup\$

I just added 3 objects to my scene to a new project. To be sure where the problem is I created a new project.

Then added to the scene: 2 Cubes and 1 Sphere 1 empty GameObject.

Cube is the target.

Sphere and Cube (1) are the Turret and both are childs of the empty GameObject. The script is attached to the empty GameObject.

The logic say if I will rotate the empty GameObject both Sphere and Cube (1) will rotate at the same time. But I found that if I'm not setting both Sphere and Cube (1) same direction before running the game they will never face to the target only the Sphere.

I had to figure out where is the Sphere facing direction before running the game and change the Cube (1) to face the same direction of the Sphere on the Z Blue axis.

Turret Sphere

Turret Cube (1)

I can't figure out yet and understand why is it so hard to rotate two objects at the same time to be facing the same direction (target).

Now it's working once the sphere and the cube (1) are facing same direction before running the game. It didn't work if I made them childs or parents or changed the Cube (1) position to 0,0,0 nothing.

They were both rotating but never faced the target only the Sphere faced the target.

So now each time I want to rotate two objects at the same time to face another gameobject like a turret, or cannon or anything that is built of two parts I need first to make them facing same direction ?

I guess I did something wrong but not sure what.

The script:

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

public class Rotateturret : MonoBehaviour
{
    public float speed = 3.0f;
    public GameObject m_target = null;
    Vector3 m_lastKnownPosition = Vector3.zero;
    Quaternion m_lookAtRotation;

    private void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (m_target)
        {
            if (m_lastKnownPosition != m_target.transform.position)
            {
                m_lastKnownPosition = m_target.transform.position;
                m_lookAtRotation = Quaternion.LookRotation(m_lastKnownPosition - transform.position);
            }

            if (transform.rotation != m_lookAtRotation)
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, m_lookAtRotation, speed * Time.deltaTime);
            }
        }
    }

    bool SetTarget(GameObject target)
    {
        if (!target)
        {
            return false;
        }

        m_target = target;

        return true;
    }
}

Here is a link for a short video clip I recorded showing what I mean:

Video clip of the problem

\$\endgroup\$
  • \$\begingroup\$ Can you edit your question and add the script you are using to rotate your turret? You should not have the problem you are currently facing. \$\endgroup\$ – Hellium Apr 4 '18 at 8:25
  • \$\begingroup\$ @Hellium Let me show you a short video clip I recorded showing the problem. I will make a new scene and will show what I did when it's not working and how I fixed it. \$\endgroup\$ – Daniel Lip Apr 4 '18 at 8:29
  • \$\begingroup\$ @Hellium Here is a link for the video: youtube.com/watch?v=my55BbeLf7A&feature=youtu.be \$\endgroup\$ – Daniel Lip Apr 4 '18 at 8:40
  • 1
    \$\begingroup\$ The foward vector is the +Z (blue). When you call LookAt, Unity will rotate the transform so that the forward vector is pointing at the given position. So align the cannon with the forward vector of your sphere. \$\endgroup\$ – Hellium Apr 4 '18 at 12:18
1
\$\begingroup\$

Set the scale of your child cube to (0.2, 0.2, 1) instead of (1, 0.2, 0.2) so that it looks like it's facing the forward/z-axis. The rotation you are getting is correct. Visually it looks like it isn't because you incorrectly scaled the turret.

\$\endgroup\$
  • \$\begingroup\$ This ties in with the comment by @Hellium The scale is (X,Y,Z) and in Unity, Z+ is forward. LookAt points the Z+ at the target. \$\endgroup\$ – Stephan Apr 4 '18 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.