Lock rotation axis Y for a following target

Hey there !

I’m working on a follow script, which is working fine, but I don’t want my following character to change it’s Y axis while going down slopes and hills, only to change the X and Z axises.

I’ve been trying to lock it through scripts like:

if (waypointTarget != null && distance > distToStop) {
			float lockPos = 0.0f;
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
			transform.rotation = Quaternion.Euler(transform.rotation.x,lockPos,transform.rotation.z);
			transform.position += transform.forward * moveSpeed * Time.deltaTime;
		}

but that didn’t work. How shall I change my original script to work as I intended ?

if (waypointTarget != null && distance > distToStop) {
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
			transform.position += transform.forward * moveSpeed * Time.deltaTime;
		}

Thanks in advance !

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointTarget.transform.position - transform.position), rotationSpeed*Time.deltaTime);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0f, transform.eulerAngles,z);
transform.position += transform.forward * moveSpeed * Time.deltaTime;

I finally managed to lock the axises so it only rotates by the Y axis.

Vector3 waypointOffset = waypointTarget.transform.position - new Vector3(0.0f,5.0f,0.0f);
			Vector3 moveDirection = waypointOffset - transform.position;

			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypointOffset - transform.position), rotationSpeed*Time.deltaTime);
			transform.rotation = new Quaternion(0.0f, transform.rotation.y, 0.0f,transform.rotation.w);

			controller.Move(moveDirection.normalized * moveSpeed * Time.deltaTime);

I also changed from using transform.position to use CharacterController.move, to check collisions and to snap to terrain and to other objects like a bridge or anything.