Look at object and locked X & Z axis?

Hi,
I want my “ball” object look at attached script object but not using X & Z axis.

public GameObject Ball;

void Update()
{

            Ball.LookAt(gameObject.transform.position); // ball look directly at attached object but X & Z should be zero

            Vector3 forward = Ball.transform.TransformDirection(Vector3.forward) * 200f;
            Debug.DrawRay(Ball.transform.position, forward, Color.green);

            RaycastHit hit2;
            if (Physics.Raycast(Ball.transform.position, forward, out hit2, 200f, 9))
            {
                print("Found an object: " + hit2.collider.name);
            }
           
}

Maybe have your Ball look at a point in the same plane with the ball’s y-coordinate, but the attached script object’s x- and z-coordinates. I haven’t run this, but it might work:

public GameObject Ball;
void Update()
{
    Vector3 myXZ = transform.position;
    myXZ.y = Ball.transform.position.y;
    Ball.LookAt(myXZ);
}
1 Like