[Help] Joy Stick Sprites visual problem
Hello. I need help. I making a mobile 2D game that uses joystick controls.
The problem is that the joystick appears very odd when its executed as seen in the Gif.
It works as intended but visually it is wrong.
The outercircle and the handle do not occupy the same place.
I do not understand what is going on.
Here is my components of the player game object just in case.
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform circle;
public Transform outerCircle;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.transform.position = pointA;
outerCircle.transform.position = pointA;
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
}
if (Input.GetMouseButton(0))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
}
private void FixedUpdate()
{
if (touchStart)
{
Vector2 offset = pointA - pointB;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction * -1);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * -1;
}
else
{
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
}
}
void moveCharacter(Vector2 direction)
{
player.Translate(direction * speed * Time.deltaTime);
}
screenshot-159.png
(83.7 kB)
gif.gif
(297.1 kB)
Comment