I have a problem with the singe joystick script usage in Unity.
I use the below script for the movement of a chopper via a rigidbody. When I test the game in unity with the arrow keys, everything works perfectly as expected. However, if I test this on a phone, the chopper only moves diagonally from bottom left to top right.
I am sure it has to do with the way I attach the joystick to the X nd Y axis, but I fail to spot my mistake....can anyone look at this with a fresh pair of eyes and tell me where I screw up?
#pragma strict
var speed : float = 500.0;
var smooth : float = 2.0;
var gravity : float = 0.0;
var moveJoystick : Joystick;
function FixedUpdate () {
var horPos = Input.GetAxis ("Horizontal") ?
Input.GetAxis ("Horizontal") : joyStickInput(moveJoystick);
var verPos = Input.GetAxis ("Vertical") ?
Input.GetAxis ("Vertical") : joyStickInput(moveJoystick);
var movedirection = new Vector3(horPos, 0, verPos);
if (movedirection != Vector3.zero){
var newRotation = Quaternion.LookRotation(movedirection * -1);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * smooth);
}
rigidbody.AddForce (movedirection * speed * Time.deltaTime);
}
function joyStickInput (joystick : Joystick) {
var absJoyPos = Vector2 (Mathf.Abs(joystick.position.x),
Mathf.Abs(joystick.position.y));
var xDirection = (joystick.position.x > 0) ? 1 : -1;
var yDirection = (joystick.position.y > 0) ? 1 : -1;
return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
}
Because horPos and verPos use the same joyStickInput(moveJoystick) ?
When press right button, your joyStickInput() will return 1 set horPos and verPos to the same value(1), and it will make your sprite moving toward top(y+1) right(x+1)