H
H
Herman Coffman2018-11-01 17:13:03
C++ / C#
Herman Coffman, 2018-11-01 17:13:03

Can you help with code (C# + Unity)?

THE CODE:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MobelController : MonoBehaviour,IDragHandler,IPointerUpHandler, IPointerDownHandler {

    private Image joysticBG;
    [SerializeField]
    private Image joystic;
    private Vector2 inputVector;

    private void Start()
    {
        joysticBG = GetComponent<Image>();
        joystic = transform.GetChild(0).GetComponent<Image>();
    }

    public virtual void OnPointerDown(PointerEventData ped)
    {
        OnDrag(ped);
    }
    public virtual void OnPointerUp(PointerEventData ped)
    {
        inputVector = Vector2.zero;
        joystic.rectTransform.anchoredPosition = Vector2.zero;
    }
    public virtual void OnDrag(PointerEventData ped)
    {
        Vector2 pos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joysticBG.rectTransform,ped.position.pressEventCamera, out pos))
        {
            pos.x = (pos.x / joysticBG.rectTransform.sizeDelta.x);
            pos.y = (pos.y / joysticBG.rectTransform.sizeDelta.y);

            inputVector = new Vector2(pos.x * 2 - 1, pos.y * 2 - 1);
            inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

            joystic.rectTransform.anchoredPosition = new Vector2(inputVector * (joysticBG.rectTransform.sizeDelta.x / 2), inputVector * (joysticBG.rectTransform.sizeDelta.y / 2));
        }   
    }
}

ERRORS IN THE CONSOLE:
error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments

error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `pressEventCamera' and no extension method `pressEventCamera' of type `UnityEngine.Vector2' could be found. Are you missing an assembly reference?

error CS1501: No overload for method `ScreenPointToLocalPointInRectangle' takes `3' arguments
 Argument `#1' cannot convert `UnityEngine.Vector2' expression to type `float'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2018-11-01
@Gera01

give a link to the original) what kind of code do you have there .. copy-pasted .. and it doesn’t work.
you didn't fully copy the errors from the console.
in the case of
ped.position.pressEventCamera,
https://docs.unity3d.com/ScriptReference/RectTrans...
see what parameters the method wants .. and what you are trying to slip it.
should be something like
I can smell the parameters when copy-pasting,
and finally, the design of questions .. code by code .. and other pleasant little things - you also need to deal with.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question