Joystick accuracy

Hi

I’m using the following script on a single joystick to move my character left/right, and then up and down into specific, pre-defined positions on the z axis.

It works, however, the movement up and down on the joystick feels a bit hit and miss. The character will move to the z positions, but not all the time. It feels that any movement at all on the x axis interferes with the y movement. So I’m wondering if there’s anyway to not allow the interruption?

function Update()
{
	var movement = Vector2.zero;
	var dir : Vector2;
	
		// horizontal movement
	if ( moveTouchPad.position.x > 0 ){
		movement = Vector2.right * forwardSpeed * moveTouchPad.position.x;
	
	} else if (moveTouchPad.position.x < 0 ) {
		movement = Vector2.right * backwardSpeed * moveTouchPad.position.x;
		
		//vertical movement
	
	} else if (moveTouchPad.position.y > 0) {
		if (thisTransform.position.z == 0) {
			thisTransform.position.z = 20;
				return;						
		} else if(thisTransform.position.z == 20) {
			thisTransform.position.z = 40;
				return;
		}
		
	} else if (moveTouchPad.position.y < 0) {
		if (thisTransform.position.z == 40) {
			thisTransform.position.z = 20;
				return;
		} else if(thisTransform.position.z == 20) {
			thisTransform.position.z = 0;
				return;
		}	
	}
	

.....

thank you

You should seperate your if-else-statements for horizontal and vertical movement. Try this:

function Update()
{
	var movement = Vector2.zero;
	var dir : Vector2;
	
		// horizontal movement
	if ( moveTouchPad.position.x > 0 ){
		movement = Vector2.right * forwardSpeed * moveTouchPad.position.x;
	
	} else if (moveTouchPad.position.x < 0 ) {
		movement = Vector2.right * backwardSpeed * moveTouchPad.position.x;
	} 

	// vertical movement

	if (moveTouchPad.position.y > 0) {
		if (thisTransform.position.z == 0) {
			thisTransform.position.z = 20;
				return;						
		} else if(thisTransform.position.z == 20) {
			thisTransform.position.z = 40;
				return;
		}
		
	} else if (moveTouchPad.position.y < 0) {
		if (thisTransform.position.z == 40) {
			thisTransform.position.z = 20;
				return;
		} else if(thisTransform.position.z == 20) {
			thisTransform.position.z = 0;
				return;
		}	
	}

b-joe, excellent, this works, thank you.

My next issue is now when the character hits the mid z position (z = 20), I can’t move horizontally as any slight movement beyond 0 on the y axis of the joystick makes it move back to either z=0 or z=40…

Think i have to sleep on a solution for this one…

joystick setups always benefit from a “dead zone” around the center. Try changing the value you are comparing the Touchpad position to from 0 to 0.05 respectivily -0.05 on the negative axis. Should have thought about that in my first reply, sorry :wink:
You will have to test to find a value that works best with your setup, but I think you get the idea.

This worked a treat, thanks again b-joe.