- Home /
Mimic Keyboard Controls with Joystick
I am trying to get a single Joystick on mobile to behave like keys. So up is up but up and right is forward and rotate right.
I think my logic is bad but have been at it for hours so looking for some help.
Thanks in advance
function Update()
{
// Keyboard Contols
if (Input.GetKey ("up"))
player.transform.Translate(Vector3.forward * Time.deltaTime * forward);
if (Input.GetKey ("down"))
player.transform.Translate(Vector3.forward * -Time.deltaTime * backward);
if (Input.GetKey ("left"))
player.transform.Rotate(Vector3.up * -Time.deltaTime * turnSpeed);
if (Input.GetKey ("right"))
player.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
//Touch Controls
// Apply movement from move joystick
var absJoyPos = Vector2( Mathf.Abs( moveTouchPad.position.x ), Mathf.Abs( moveTouchPad.position.y ) );
// Movement
if ( absJoyPos.y > absJoyPos.x )
{
if ( moveTouchPad.position.y > 0 && absJoyPos.x > 0){
player.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}
if ( moveTouchPad.position.y < 0 && absJoyPos.x < 0){
player.transform.Rotate(Vector3.up * -Time.deltaTime * turnSpeed);
}
player.transform.Translate(Vector3.forward * Time.deltaTime * forward);
}
if ( absJoyPos.y < absJoyPos.x ){
if ( moveTouchPad.position.y > 0){
player.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}
if ( moveTouchPad.position.y < 0 ){
player.transform.Rotate(Vector3.up * -Time.deltaTime * turnSpeed);
}
player.transform.Translate(Vector3.forward * -Time.deltaTime * backward);
}
if ( absJoyPos.x > absJoyPos.y )
{
if ( moveTouchPad.position.x > 0 ){
player.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}
if ( moveTouchPad.position.x < 0 ){
player.transform.Rotate(Vector3.up * -Time.deltaTime * turnSpeed);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Spaceship movement 2 Answers
Mobile Virtual Joysticks / Keyboard Script Conversion 0 Answers
Get smooth input when Time.timescale == 0 1 Answer
Which axis to set in the InputManager? 0 Answers