- Home /
Doing something wrong with transform.position and transform.Translate
Here's my script:
using UnityEngine;
using System.Collections;
public class AspRatCheck : MonoBehaviour {
void Start () {
// Declares variable for aspect ratio and Boolean variables that check for aspect ratio equality
float BordX = 0;
float ScreenAspect = Screen.width / Screen.height;
bool check169 = ScreenAspect == 16.0f / 9.0f;
bool check1610 = ScreenAspect == 16.0f/ 10.0f;
bool check219 = ScreenAspect == 21.0f / 9.0f;
bool check43 = ScreenAspect == 4.0f / 3.0f;
bool check54 = ScreenAspect == 5.0f / 4.0f;
// Checks all Boolean variables to find current display aspect ratio and assigns value to BordX
if (check169) {
BordX = 3.75f;
} else if (check1610) {
BordX = 3.4f;
} else if (check219) {
BordX = 4.9f;
} else if (check43) {
BordX = 2.85f;
} else if (check54) {
BordX = 2.65f;
}
// Declares Vector3 for border transform coordinates and sets transform coordinates
Vector3 borderVect = new Vector3(BordX, 0.0f, 0.0f);
transform.Translate(borderVect);
}
}
It's basically meant to check which aspect ratio the display is using (I'm forcing the Unity game view to use one of them) and position 2 game borders accordingly, but it just doesn't. Nothing happens, no matter what aspect ratio I'm using. I've tried both "transform.position = borderVect" and the above to no avail. I'm only a beginner with C# so I have no idea what I'm doing wrong, can someone please help me?
Answer by MrKagouris · Oct 08, 2016 at 08:39 PM
As it turns out ScreenAspect was getting an integer value because Screen.width and Screen.height are both integers, so I had to set separate float variables for both and then divide those variables instead, and also use transform.position.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to shift the player to a definite position in the x-axis using key presses? 1 Answer
noobNeedsHelp with writing script for movement 0 Answers
My object is translating with speed but does not stop... 1 Answer
Align Parent object using child object as point of reference 3 Answers
changing x multiplies x,y and z position for no reason 2 Answers