RectTransform offset renders wrong (UnityAndroid)
Using latest unity2d (2017.1.1f1), developing for android
When I put my finger on the screen, I place the RectTransform under my finger, and the calculations are perfect! The RectTRansform goes exactly where it should.
Problem is that even if I set offsets to 0, the sprite is drawn far away from the RectTransform itself.
As you can see in the image provided, the anchors and offset place the sprite in a certain position, but it is drawn elsewhere.
Does anybody have a solution for this?
Additional infos:
working on Windows
the issue happens on both mobile and editor
it did happen even with previous versions of unity
Here's the code I use to set the position of the element:
bool aiming = false; // to check whether the finger is on screen or not (there's also a check for the "moving" button)
foreach (Touch t in Input.touches) { // checking among ALL fingerpress
if (t.position.x >= Screen.width/2) { // if the finger is in the right part of the screen
if (t.phase == TouchPhase.Began) { // and the touchphase has just begun
aimCenter = t.position; // set aimCenter (Vector3) to touch.position
Vector2 pos = new Vector2 (aimCenter.x / (float)Screen.width,aimCenter.y / (float)Screen.height); // calculate the RectTransform anchors (center)
actionHandler.SetPosition (pos.x - .08f, pos.y - .14f, pos.x + .08f, pos.y + .14f); // my own method to set anchors to given paramenters (x1,y1,x2,y2) and then set offsets to 0 (actually works)
actionHandler.gameObject.SetActive (true); // make object visible, actionHandler is the RectTransform itself
}
aiming = true; // actually aiming, I use this to check if I should set the gameObject to inactive (hide it)
}
}
Your answer
Follow this Question
Related Questions
Inconsistent visibility of small sprites 0 Answers
Trouble getting created sprite to set. 0 Answers
How to place my own UI Image? 1 Answer