Unity: How can I pan through a scene only on the y-axis?

Viewed 102

I have this code here that pans my scene with the camera on both the x-axis and y-axis. I've changed my mind and only want to pan down and up on the y-axis but keep the x-axis the same, how could I edit the script to do so? Thanks! :)

void LateUpdate () {
    if (Input.GetMouseButtonDown(0)){
        touchStart = GetWorldPosition(groundZ);
    }
    if (Input.GetMouseButton(0)){
        Vector3 direction = touchStart - GetWorldPosition(groundZ);
        cam.transform.position += direction;
    }
}
private Vector3 GetWorldPosition(float z){
    Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
    Plane ground = new Plane(Vector3.forward, new Vector3(0,0,z));
    float distance;
    ground.Raycast(mousePos, out distance);
    return mousePos.GetPoint(distance);
}
1 Answers

It has been a while since i have used unity and i might just be wrong. But i think you can just remove the x and z components from the direction vector.

void LateUpdate () {
    if (Input.GetMouseButtonDown(0)){
        touchStart = GetWorldPosition(groundZ);
    }
    if (Input.GetMouseButton(0)){
        Vector3 direction = touchStart - GetWorldPosition(groundZ);
        direction.x = 0;
        direction.z = 0;
        cam.transform.position += direction;
    }
}
private Vector3 GetWorldPosition(float z){
    Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
    Plane ground = new Plane(Vector3.forward, new Vector3(0,0,z));
    float distance;
    ground.Raycast(mousePos, out distance);
    return mousePos.GetPoint(distance);
}
Related