The question is answered, right answer was accepted
GameObjects not spawning at specified position when the resolution is changed
I have a problem when instantiating a game object into the game via script. I checked manually where the object has to be in order to sit perfectly in bottom-right corner of the screen. I wrote the script that instantiates that object at the same location. As long as I starting the scene in the same resolution i had while checking the values, everything is great, but whenever I try something with different aspect ratio, the object spawns in a different place due to the change of camera viewing size. I've tried making a reference to the camera and checking pixelWidth and pixelHeight, then making an aspect ratio out of them and spawning the object at the same point, but multiplied by the ratio, but it didn't resolve the problem, although it made it less apparent. Thanks for the help in advance. Edit: the code that was supposed to instantiate the GameObjects
public class CharacterPlacements : MonoBehaviour
{
Vector3 right;
Vector3 left;
Vector3 middle;
void Start()
{
right.Set( 7.5f, -4.5f, 0); // These coordinates are working fine at 16:10
left.Set(-7.5f, -4.5f, 0);
middle.Set(0, 0, 0);
}
public void Create(string characterName, string where)
{
GameObject character = Instantiate(Resources.Load(characterName, typeof(GameObject))) as GameObject;
character.name = characterName;
if(where == "r")
character.transform.position = right;
if (where == "m")
character.transform.position = middle;
if (where == "l")
character.transform.position = left;
}
Hello $$anonymous$$cBowie.
If you have problems with some script, you must paste it here (Use code sampler or Crtl+$$anonymous$$). If not is impossible to know what are you doing...
When Instantiate, you must give a position in worldspace, so camera resolution is not a variable at all, unless you are finding the worldposition in reference with some mouse/canvas property...
Please post your code.
Bye.
Answer by tormentoarmagedoom · May 23, 2019 at 10:34 AM
Hello again.
Are this vector 3 variables the coord in world space? I think the prolem is that when you change resloution, you see the world different, but the characters are instantiated at same world point...
Can you show some images?
As i see in this script is, you have 3 points in the world scene:
Then you just instantiate and move the charcater to oine of this positions.
Resolution of the camera does not change anythig, only change what you see... so if you say they are moving, i say no, they are not moving.. you are changent the prespective, the field of vision, the zoom.... but the object are still in same palce in the world scene.
IF what you want is to instantiate always in the corners of the screen in all aspect ratios, you need to change the position in relation to your screen, not the wolrd coords.
So you need to use thie function ScreenToWorld so you take some position of screen as reference, and from there, you can calculate the world position where to move the character.
Good luck!
Thank you so much! This pushed me into the right direction.
The way I solved it was: I read the camera's width, then I use it as a X coordinate for in a variable, which i feed to the ScreenToWorldPoint function and then add the offset deter$$anonymous$$ed by my character's width, and now it works like a dream!
Thanks a milion!