- Home /
transform.Translate doesnt travel the same distance for multi-reso
Hello
this transform.Translate code make my game object move to the negative y axis
void Start()
{
ms = -700f;
}
void Update ()
{
transform.Translate (0, ms*Time.deltaTime, 0);
}
but the problem is when i change the resolution of the game the object moves slower, so i decided to make the movement speed depends on the height of the screen, so for example if we have a screen with height 500px ms will be 250px/s and if the height is 1000px ms will be 500px/s
also i changed the Canvas Scaler to Scale with screen size 800*600 so a screen with 2:3 aspect ratio will have 800*1200 screen size. so and i made this code
HoverW = ((float)Screen.height / (float)Screen.width);
newHeight=800*HoverW; // 800 is the resolution refrence from canvas
ms=newHeight;
so i thought from the above code the object will travel the screen height in 1 time unit and doesnt depend on the resolution, but unfortunately in some resoultions the object travels it in less or more than 1 time, i mean the object doesnt travel the whole height in on the same time for multi-reso
i have tried to just do Screen.height as ms and didnt work also :(
any help please
I imagine it is going the same speed regardless of resolution. You will find that Unity likes to adjust the viewport which changes the amount of actual game-world 'real-estate' that can fit into your screen, in order to preserve image quality.
then how can i balance the object speed from device to another?
cause i tested the game on my ipad and the object was very very slow while in the editor its speed was very fast maybe 5x or 6x the ipad speed
hmmm with that much of a difference I wonder if it's maybe a performance issue? Are there a lot of things going on besides the moving game object?
$$anonymous$$aybe try using FixedUpdate ins$$anonymous$$d of Update and Time.fixedDeltaTime ins$$anonymous$$d of Time.deltaTime ?
Answer by daneislazy · Feb 08, 2015 at 04:53 PM
Translate should not be affected by the screen resolution or aspect ratio. I would guess, as meat5000 mentioned, that the amount of game world being shown is changing when the screen resizes so it just looks like it is moving at different speeds.
You can do some visual testing by putting a big Plane object behind the object you are moving so that you have a reference point, maybe with a repeating texture as well.
You can also make a second similar game object with a rigidbody
attached to it. Turn off Gravity and set the drag to 0. Then, add rigidbody.velocity = Vector3.down * speed;
into the Awake()
method in a new script for it. And set speed to whatever you need it to go. (probably 700 based off of your initial example, also Vector3.down is already negative) Then you can compare the two speeds to see if something is wrong.
Edit: with regards to the question maker's comment on different platforms
You shouldn't need to worry too much about it because the screen resolution/ viewport size doesn't affect the positions of objects in the game world.
But if it's something to do with the how much can be seen or something...
If you are using an Orthographic camera the Size is in units from the center of the camera to the top/bottom of the screen so yes, changing the aspect ratio will change the amount that can be seen on the sides.
You could possibly add some in game "black bars" to the sides of the camera so that if it is too wide you just see black. But some people don't like that.
If you are using a perspective camera it's similar but uses a default of 60 degrees angle, again from the bottom of the screen so only the sides change.
Either way you shouldn't really need to change the game mechanics based on the resolution or aspect ratio unless there is some huge flaw in the camera system that I am overlooking.
Answer by xdarned · Jan 17, 2021 at 01:29 AM
Hi, I will post this here since there is no other place where a similar question exists. This is not a solution! I have a canvas set to scale with screen size, and have a rectTransform inside the canvas. Anchors are set to center as well as pivot. Inside that rectTransform there is a child wich is an image. This child (rectTransform as well) has pivot and anchors set to center. I have a script:
float speed; //Set from inspector
void Update{
transform.Translate(Vector3.down * speed);
}
Now, this is the thing: If I run the game at full screen, the image moves slow. BUT if I run it windowed (small) the image moves realy fast.
This looks like the problem of OP to wich the provided solution didn't work.
So it looks like the problem is inside the transform.Translate itslef. I have tried to set speed directly dependant on screen size but no results. I will try another method for movement and share results if succed...