Hi,
I cannot have any other values than -1,0,1 for the joystick. I changed the sensitivity, gravity and dead of the joystick Horizontal like this :
I tried different settings for example using Key or Mouse Move instead of Joystick Axis, but nothing works.
In my Update, I have this :
Thanks, now when I go right with the joystick, it says 1. But when I stop pushing the joystick, it does not come back to 0 but to -0.01077548 (both getAxisRaw and getAxis).
I found a way to make it work! (a little bit) as the -1 is in the middle of the joystick, but at least I have got float values! I had to press “Analog” on the joypad, and put sensitivity really low, like 0.01.
I am trying to have the joystick being -1 only if the joystick is at the maximum on the left, now it is -1 at the middle… if you know a good way. (I am not sure about what settings to add in gravity/sensitivity).
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public static Dictionary<Type, Joystick> instance=new Dictionary<Type, Joystick>();
public enum Type{
Left,Right
}
public enum AxisOption
{
// Options for which axes to use
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public Type type;
public int MovementRange = 100;
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
Vector3 m_StartPos;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
public float X;
public float Y;
//CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
//CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
void Awake()
{
if (instance.ContainsKey (type))
instance.Remove (type);
instance.Add (type, this);
}
void Start() //DCURRY: Changed to Start from OnEnable
{
m_StartPos = transform.position;
CreateVirtualAxes();
}
public Vector2 GetAxis()
{
return new Vector2(X, Y);
}
void UpdateVirtualAxes(Vector3 value)
{
var delta = m_StartPos - value;
delta.y = -delta.y;
delta /= MovementRange;
if (m_UseX)
{
X=-delta.x;
}
if (m_UseY)
{
Y=delta.y;
}
if (Mathf.Abs(X) > 0 || Mathf.Abs(Y) > 0)
{
Vector2 screenSize = new Vector2(Screen.width/2 , Screen.height /2);
Vector2 directScreenPos = Vector3.zero;
if (X < 0)
{
directScreenPos.x = screenSize.x * (1 + X);
}
else if (X > 0)
{
directScreenPos.x = screenSize.x + screenSize.x * X;
}
if (Y < 0)
{
directScreenPos.y = screenSize.y * (1 + Y);
}
else if (Y > 0)
{
directScreenPos.y = screenSize.y + screenSize.y * Y;
}
ControlDirectionMove.UpdateDirection(directScreenPos);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
//delta = Mathf.Clamp(delta, - MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
//delta = Mathf.Clamp(delta, -MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
newPos.y = delta;
}
//DCURRY: ClampMagnitude to clamp in a circle instead of a square
transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
UpdateVirtualAxes(transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
public void OnPointerDown(PointerEventData data) { }
}