AI Look at X axis more efficient code

are there any alternatives for this?

but I want it to look only through X as I’ve got to move it around the planet, …

The code works but it would be easyer if it would take a line or 2 so it’s not that much spammy, …

sorry I still suck at trigonometry

	Target = TargetTransform.position;
	
	if (Target != Vector3.zero){
		Vector3 Temp = transform.localEulerAngles;
		transform.LookAt(Target);
		Vector3 Temp1 = transform.localEulerAngles;
		Temp.y = Temp1.y;
		transform.localEulerAngles = Temp;
	}

The method you’re using is more or less correct, but you’re just doing it in a very roundabout way, and that makes it less efficient. Your main issue is that you’re creating two temporary variables when you only really need one. A lot of code optimisation just lies in reducing the number of times you have to shift memory about, and by using extra temporary variables, you add a lot onto that.

So, removing that extra variable and condensing it down, we get this:

if (Target != Vector3.zero)
{
       Vector3 t = transform.localEulerAngles;
       transform.LookAt(Target);
       t.y = transform.localEulerAngles.y;
       transform.localEulerAngles = t;
}

This does exactly the same thing, but notice how it skips the step of creating a new variable, in favour of editing the original one. In general, if you only need to use a variable once, then there’s not much point in making it in the first place - you might as well just move the expression and save allocating memory. The only time you don’t want to do this is if your variable is keeping track of some state, in order to use it later, after that state has been changed. An example would actually be the use of the first temporary variable here, as, while you only use it once, it keeps track of a value that would otherwise not exist by the time you use it.

There’s not much more you can do to this to make it more efficient if you want to use this method. There are other ways to do it, though, by using vector mathematics and Quaternion operations, but those are a little more complicated, and really, this isn’t particularly inefficient, so it should be fine.