W
W
WasTabon2021-04-12 15:13:13
Unity
WasTabon, 2021-04-12 15:13:13

Why does the UI button fly away on the Z coordinate?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UpDownButtons : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
    public Camera cam;
    public void OnPointerUp(PointerEventData eventData)
    {
        gameObject.transform.position = cam.ScreenToWorldPoint (new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y + 6f, 0));
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        gameObject.transform.position = cam.ScreenToWorldPoint(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 6f, 0));
    }
}

When pressed, it immediately becomes 350 + - in z, and 0 in y and x

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
namee, 2021-04-12
@namee

Can the camera coordinates have left? Or at the canvas

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question