- Home /
Question by
steveeltham · Mar 17, 2019 at 09:57 PM ·
androidtouchpadreturn value
Space Shooter tutorial - using touchPad.GetDirection()
Hi,
In the following code the function GetDirection() always has direction set to (0,0). I checked that with the debug.log which is currently commented. There is another debug.log of direction in OnDrag() and that shows changing values. So... I'm really stuck as to why GetDirection() cannot locate the correct value of direction.
Can anyone help please? Cheers, Steve
public void OnDrag(PointerEventData data)
{
// compare difference between start point and current
Vector2 currentPosition = data.position;
Vector2 directionRaw = currentPosition - origin;
Vector2 direction = directionRaw.normalized;
Debug.Log(direction);
}
public void OnPointerUp(PointerEventData data)
{
// reset everything
direction = Vector3.zero;
}
public Vector2 GetDirection()
{
//Debug.Log(direction);
//direction = new Vector2(-0.9f, -0.9f);
return direction;
}
Comment
Answer by steveeltham · Mar 18, 2019 at 12:07 PM
This seems to have been a bug in the tutorial. In OnDrag() I changed
Vector2 direction = directionRaw.normalized;
to
direction = directionRaw.normalized;
and that has fixed the problem :-)