- Home /
[mobile development] 2D Joystick Controller
hi everyone, first i'm sorry for mistake i can do with my english ! I've a problem with my code about mouvement for my little game. the movement i want is on X and Y axis. I've used the standart assest for mobile, the CameraRelativeController. but he doing so much things for my game, so I've edit the code. because my player can move on a 3D environment, and it's a 2D game, as a JetPack Joyroid movement.
The player can move correctly on X axis, but i don't know how to move on Y axis. I hope you can help me to solve this. I you need to know, this is the only code on Js, i'm scripting on C#
The new code on CameraRelativeController :
#pragma strict
// This script must be attached to a GameObject that has a CharacterController
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var cameraTransform : Transform; // The actual transform of the camera
var speed : float = 5; // Ground speed
private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3; // Used for continuing momentum while in air
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function OnEndGame()
{
// Disable joystick when the game ends
moveJoystick.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, moveJoystick.position.y, 0 ) );
movement.Normalize(); // Adjust magnitude after ignoring vertical movement
// position of joystick
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
///
/// The code I think isn't working
/// initial code : ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
///
movement *= speed;
movement.x *= absJoyPos.x;
movement.y *= absJoyPos.y;
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
}
thanks !!
Your answer
![](https://koobas.hobune.stream/wayback/20220613142931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make Dual Joysticks for Android and IOS? 1 Answer
Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers
Xbox 360 Controller on macbook pro problem 0 Answers
Map mobile joystick to Input.GetAxis("Horizontal") and "Vertical" 1 Answer
help with First Person mobile game 1 Answer