Hello Guest

Author Topic: Programmatically positioning within camera view  (Read 4048 times)

mygamingproject

  • Newbie
  • *
  • Posts: 37
    • View Profile
Programmatically positioning within camera view
« 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Programmatically positioning within camera view
« Reply #1 on: August 24, 2014, 01:10:52 pm »
What camera settings are you using?

mygamingproject

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Programmatically positioning within camera view
« Reply #2 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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Programmatically positioning within camera view
« Reply #3 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.

mygamingproject

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Programmatically positioning within camera view
« Reply #4 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);