public Transform targot;
public Transform start;
public Transform end;
void Start()
{
}
void Update()
{
Vector3 normal = (end.position - start.position).normalized;
Vector3 pos = start.position + Vector3.Project(targot.position- start.position, normal);
pos.x = Mathf.Clamp(pos.x, Mathf.Min(start.position.x, end.position.x), Mathf.Max(start.position.x, end.position.x));
pos.y = Mathf.Clamp(pos.y, Mathf.Min(start.position.y, end.position.y), Mathf.Max(start.position.y, end.position.y));
pos.z = Mathf.Clamp(pos.z, Mathf.Min(start.position.z, end.position.z), Mathf.Max(start.position.z, end.position.z));
transform.position = pos;
transform.LookAt(targot);
}