Hi,
I would like to moves a cursor in screenspace, using my xbox360 joystick, and have it stops on the edge of the screen.
My control are setuped, i can get axis value, but i have no idea how to actually map the -1 to 1 value of the axis to position in the screen.
It must be simple, but i cannot figure it out !
thank you
Depending how fast you want to move you can just add up things. The following example uses hardcoded full hd resolution. If your resolution differs from that, you should get your game’s resolution dynamically.
void Update()
{
Vector2 input = new Vector2(Input.GetAxis("YourHorizontalAxis", "YourVerticalAxis")
RectTransform rectTransform = YourUIObject.GetComponent<RectTransform>()
input = CheckInput(rectTransform, input)
rectTransform.anchoredPosition += new Vector3(input.x, input.y, 0f) * yourWishedSpeed;
}
// in case we reach a border, only allow adjusted input
Vector2 CheckInput( RectTransform rectTransform, Vector2 input)
{
if ( rectTransform.anchoredPosition.x >= 1920 )
{
if(input.x > 0) input.x = 0;
}
if ( rectTransform.anchoredPosition.x <= 0)
{
if(input.x < 0) input.x = 0;
}
if ( rectTransform.anchoredPosition.y >= 1080 )
{
if(input.y > 0) input.y = 0;
}
if ( rectTransform.anchoredPosition.y <= 0 )
{
if(input.y < 0) input.y = 0;
}
return input;
}