- Home /
Fixed but don't know what fixed it.
Joystick unusual behavior on axis with almost the same script as W A S D
Hello, after days of struggling ... here i am. So basically my problem is as follows:
I control a sphere in a scene. Sphere goes right by dragging the joy right, left by dragging to the left, goes forward (related to camera because i have a side view) if pulled up and comes towards camera if pulled down. The problem is that somehow dragging it to the right has super speed, dragging it to the left has super slow speed, dragging it up and down moves a little bit diagonally to the right (the direction with super boost for my speed). I have to mention that the WASD controls work perfectly and as you will see they are written almost identically...
Those are my codes:
Movement script attached to sphere: using UnityEngine;
public class MavenMovementControl : MonoBehaviour {
//Maven object
public Rigidbody maven;
//Joystick controller
public FixedJoystick joystick;
//Values to control Maven
public float acceleration;
public float groundedDistance;
public float groundedDrag;
public float airborneDrag;
//Values for faking a nice turn display
public float turnRotationAngle;
public float turnRotationSeekSpeed;
//Refference variables:
private float rotationVelocity; //undefined yet
private float headingHorizontal;
private float headingVertical;
private float heading;
private Vector3 joystickDirection;
private void Start()
{
maven = GetComponent<Rigidbody>();
}
private void Update()
{
headingHorizontal = Input.GetAxis("Horizontal");
headingVertical = Input.GetAxis("Vertical");
//Maven is facing joystick direction
if (FixedJoystick.isPressed)
{
heading = Mathf.Atan2(joystick.Vertical, -joystick.Horizontal) * Mathf.Rad2Deg;
}
//if (keyPressed)
//{
// joystickDirection.x = Input.GetAxis("Horizontal");
// joystickDirection.y = Input.GetAxis("Vertical");
// transform.Translate(joystickDirection * Time.deltaTime, Space.Self);
// heading = Mathf.Atan2(joystickDirection.y, -joystickDirection.x) * Mathf.Rad2Deg;
//}
}
void FixedUpdate () {
//Ground checker ray debug
Debug.DrawRay(transform.position, transform.up * -5, Color.blue);
//Check if Maven is touching the ground:
if (Physics.Raycast(transform.position, transform.up * -1, groundedDistance))
{
//Debug grounded
Debug.Log("Is grounded!");
JoyButtonJump.jumpInProgress = false;
//Maven is on the ground; enable accelerator and increase drag:
maven.drag = groundedDrag;
//Calculate forward force:
Vector3 forwardForceJoy = acceleration * new Vector3(joystick.Vertical, 0, -joystick.Horizontal);
// Vector3 forwardForceKey = acceleration * new Vector3(headingVertical, 0, -headingHorizontal).normalized;
//Correct force for deltatime and vehicle mass:
forwardForceJoy = forwardForceJoy * Time.deltaTime * maven.mass;
// forwardForceKey = forwardForceKey * Time.deltaTime * maven.mass;
//Add the force to Maven object
maven.AddForce(forwardForceJoy);
// maven.AddForce(forwardForceKey);
}
else
{
maven.drag = airborneDrag;
JoyButtonJump.jumpInProgress = true;
Debug.Log("Is not grounded");
}
//Fake rotate maven when you are turning:
rotationVelocity = maven.velocity.magnitude;
Vector3 newRotation = maven.transform.eulerAngles;
newRotation.z = Mathf.SmoothDampAngle(newRotation.z, joystick.Vertical * -turnRotationAngle, ref rotationVelocity, turnRotationSeekSpeed);
newRotation.x = Mathf.SmoothDampAngle(newRotation.x, joystick.Horizontal * turnRotationAngle, ref rotationVelocity, turnRotationSeekSpeed);
maven.transform.rotation = Quaternion.Euler(newRotation.x, heading, newRotation.z);
}
}
And this is the code for my joystick...
using UnityEngine;
using UnityEngine.EventSystems;
public class FixedJoystick : Joystick
{
Vector2 joystickPosition = Vector2.zero;
public static bool isPressed = false;
void Start()
{
joystickPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickPosition ;
inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
ClampJoystick();
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
isPressed = true;
}
public override void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector2.zero;
isPressed = false;
handle.anchoredPosition = Vector2.zero;
}
}
Hmm... What is the value of background.sizeDelta in your script? It *might* not be the value you need to use in this situation, but I don't recall all the intricacies of RectTransforms from memory at the moment.
This is the base joystick script. I forgot about it..... It gets the value from the actual background of joystick.
using UnityEngine;
using UnityEngine.EventSystems;
public class Joystick : $$anonymous$$onoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
[Header("Options")]
[Range(0f, 2f)] public float handleLimit = 1f;
public Joystick$$anonymous$$ode joystick$$anonymous$$ode = Joystick$$anonymous$$ode.AllAxis;
protected Vector2 inputVector = Vector2.zero;
[Header("Components")]
public RectTransform background;
public RectTransform handle;
public float Horizontal { get { return inputVector.x; } }
public float Vertical { get { return inputVector.y; } }
public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }
public virtual void OnDrag(PointerEventData eventData)
{
}
public virtual void OnPointerDown(PointerEventData eventData)
{
}
public virtual void OnPointerUp(PointerEventData eventData)
{
}
protected void ClampJoystick()
{
if (joystick$$anonymous$$ode == Joystick$$anonymous$$ode.Horizontal)
inputVector = new Vector2(inputVector.x, 0f);
if (joystick$$anonymous$$ode == Joystick$$anonymous$$ode.Vertical)
inputVector = new Vector2(0f, inputVector.y);
}
}
public enum Joystick$$anonymous$$ode { AllAxis, Horizontal, Vertical}
So my background sizeDelta is constantly 300 in Debug.Log
Also input vectors are correct.. 0.9999 all axis when pulled to max... getting reset when joystick is released
Hmm... This is quite the conundrum. Looking over everything, it all seems like it should be working as intended. If nothing about the UI is getting resized, the (virtual) joystick center shouldn't be moving anywhere (although UI updates co$$anonymous$$g at the end of a frame have been my bane before). Regardless, you mention that joystick input all appears correct.
The one thing left that comes to $$anonymous$$d, just to be sure, the line among keyboard controls...
transform.Translate(joystickDirection * Time.deltaTime, Space.Self);
... is not factored in, is it? If it were, it would just have misleading results rather than anything actually catastrophic.