Changing the GUI based on camera position

Hey all,

I’m still continuing my seemingly never-ending battle with my text-based game.

Just a few notes -

・A page consists of a texture stuck on a GameObject.

・Screen resolution is set to 1024 x 1024 and the camera is positioned so that the page takes up the entire screen.

・You can swipe left / right to turn the page. I’m not really worried about a page-turn animation at this point (handled through void Update.)

My problem is this. On each “page” (i.e. each game object) there are different keywords that the player can touch to find out more about a given subject. Because the words don’t really exist (since the pages are simply textures), I was thinking that I’d need to stick GUI Buttons around each keyword that the player can touch.

The problem is that if I do that through the GUI - then I’d need to come up with a system that would change which buttons are displayed depending on the location of the camera.

For example, if the camera is at 0,0,-500 (page 1) then I’d need Box A at position A to appear. If the camera is at -1024, 0, -500 (page 2) then I need Box B to appear at position B.

I have no idea how to do this. Keep in mind that there will likely be around a 100 or so pages (textured GameObjects), each one requiring it’s own setting.

Any suggestions? I’ve looked all over the place but I can’t seem to find any other examples of similar projects and my programming experience is very limited.

Really appreciate any help you can give. :confused:

When someone clicks on one of these keywords, what do you expect it to do? Just go to another page? Also, do you have any special code per page, or do you just simply have code that detects the swipes and you have a bunch of textures lined up in Unity to do the movement?

1 Answer

1

This would get you started on being able to “link” to other pages.

Since you’re putting a material on a simple plane, what you could do is put a plane that acts as your clickable area (with it’s rendered disabled) in the scene as well.

You would want to create a few more scripts:

  • Button: this gets placed on every plane you want to be clickable. Button should have a variable public GameObject linkObject that you can drag the page you want the button to link to.

To make the planes disappear whenever the game starts, simply put this in the Button script:

void Start () {
	renderer.enabled = false;
}
  • InputManager: this is where you would move your “GetMouseUp” code, and attach it to the camera.

Your InputManager should have your code, and this in it’s Update():

if (Input.GetMouseButtonUp(0))
{
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hitInfo;

	if (Physics.Raycast(ray, out hitInfo, 100f))
	{
		Button hitButton = hitInfo.collider.gameObject.GetComponent<Button>();
		if (hitButton != null && hitButton.linkObject != null)
		{
			Vector3 movementVector = hitButton.linkObject.transform.position - Camera.main.gameObject.transform.position;
			movementVector.z = 0;
			Camera.main.transform.Translate(movementVector);
		}
	}
}

What this does is cast a ray from where you click, into the scene. If it hits a plane with a “Button” script attached and that Button has a linkObject defined, it’ll move the camera immediately to that position, without moving it on the Z axis (which I’m assuming is your depth.)

Hope this helps!