Quaternion.LookRotation() problem.

Hello

I am trying to make a top down 2D game with the orthographic view and trying to make the turret of a tank follow an object attached to the mouse position.

my code kinda works but i dont get a 360 degree spin it can only rotate 180 degrees and then turns the opposite direction to the object i want it to follow.

also i had to mees up the model direction as in the code below X is faceing away from the camera,
but when i have Z facing away from the camera i do not get anyrotation at all even when i dont set Y to a constant.

this is the code i am using

using UnityEngine;
using System.Collections;

public class Followcureser : MonoBehaviour {

	private Transform target;


	// Use this for initialization
	void Start () 
	{

		//cube is an object that follows the mouse curser
		target = transform.FindChild("Cube");
	}
	
	// Update is called once per frame
	void Update () 
	{
		var newRotation = Quaternion.LookRotation(target.position - transform.position).eulerAngles;
		newRotation.y = 90;
		//newRotation.x = 270;
		newRotation.z = 0;
		transform.rotation = Quaternion.Euler(newRotation);
	}
}

i think its because the rotation is based off of the distance between the two objects but i am not sure.

I would like the object that has this attached, to look at the target object no matter where it is in a 2D plane.

1574601–93452–$Followcureser.cs (552 Bytes)

private void Update()
{
    transform.rotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
}

There. :slight_smile: