I can never get transform.lookat to work. Can someone help?

No matter what I try, the function just doesnt work for me. It either rotates the wrong axis, doesnt rotate at all, just jitters randomly or the whole object gets another position? I seriously dont know what Im doing wrong.

 void lookAtTarget()
    {
        Vector3 targetVec = target.transform.position;
        Vector3 ownVec = transform.position;


        baseTop.LookAt(new Vector3(0, 0, targetVec.z), Vector3.forward);
    }

Im trying to rotate it on 1 axis only, right now it takes a different position and rotates on the wrong axis. Ive tried every combination of direction, constraints etc etc. Please Im getting so annoyed by this.

The LookAt() call will make the +Z axis (blue arrow) face the target.

It obviously needs an implicit “up” and will use Vector3.up if you don’t provide it.

However, you are providing it as the second argument, which you are providing as Vector3.forward.

This is the reason it’s not working for you: you are saying “consider forward” to be up, and then look at (0,0,z) of your target. Just omit the second argument.