I cannot for the life of me figure this out. I have a script that works with 2 circle images to produce a on screen joystick. In the first scene I created it works perfect but when I try to use the exact same prefab Camera/Joystick I cannot get it to work in another scene. The Circle no longer moves with the mouse click or at all. Exact same script, exact same Canvas/Images/Names. No matter what I try it only works in the original scene. Please tell me what I am doing wrong here! Thanks for your time ![]()
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgjoystick;
private Image Joybutton;
private Vector3 inputVector;
private void Start()
{
bgjoystick = GetComponent<Image>();
Joybutton = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(bgjoystick.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / bgjoystick.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgjoystick.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
Joybutton.rectTransform.anchoredPosition = new Vector3(inputVector.x * (bgjoystick.rectTransform.sizeDelta.x / 3), inputVector.z * (bgjoystick.rectTransform.sizeDelta.y / 3));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
Joybutton.rectTransform.anchoredPosition = Vector3.zero;
}
public float Horizontal()
{
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (inputVector.z != 0)
return inputVector.z;
else
return Input.GetAxis("Vertical");
}
}
The only thing I can think of is somehow it is no longer pulling the image of the Joystick as a child but it is still the only child attached to the bgimage. The script itself is attached to the bgimage. Any suggestions greatly appreciated, again thanks! ![]()