2D Toolkit Forum

2D Toolkit => Support => Topic started by: mygamingproject on August 24, 2014, 11:31:01 am

Title: Programmatically positioning within camera view
Post by: mygamingproject on August 24, 2014, 11:31:01 am
I think I am probably making this difficult for myself but I simply want to spawn a prefab in a position that is always within the camera view.

The issue I think I am having is I'm confusing pixels with Screen points in my positioning so some appear and some others don't but nonetheless can't work it out.

Any assistance would be appreciated.
Title: Re: Programmatically positioning within camera view
Post by: unikronsoftware on August 24, 2014, 01:10:52 pm
What camera settings are you using?
Title: Re: Programmatically positioning within camera view
Post by: mygamingproject on August 24, 2014, 03:51:48 pm
Its a tk2dCamera

With the following settings:

http://imgur.com/L5QpgpO
http://imgur.com/KJxMxLh
http://imgur.com/5SlH4lU

Setup to handle iDevice resolutions
Title: Re: Programmatically positioning within camera view
Post by: unikronsoftware on August 24, 2014, 09:54:22 pm
camera.transform.position will put something at the bottom left of the camera.
you can then use tk2dCamera.ScreenExtents to work out the extents of the complete visible area of the camera.
Title: Re: Programmatically positioning within camera view
Post by: mygamingproject on August 24, 2014, 10:39:49 pm
Yep that did it thanks

My camera origin was actually marked as center so I came up with the crude code as follows which works

Code: [Select]
Vector3 cameraPosition = camera.transform.position;

Rect extents = camera.ScreenExtents;
float screenX = Random.Range(cameraPosition.x -  (extents.width * 0.5f), cameraPosition.x + (extents.width * 0.5f));

blockPrefab.Spawn(new Vector3(screenX, 26.0f, 0.0f), transform.rotation);