- Home /
Problems Player turning in mobile controls in FPS mode
I am having problem with the player turning. My project is a first person shooter. The player is depicted as just his arms and gun. I have been trying to get mobile controls working properly. With the mobile pack out of unity standard assets The screen controls work as WASD works.And also the fire button. But when the player is moving he doesn't turn to face the direction moving. Without the mobile controls enabled the player turns when using the keyboard WASD for movement and the mouse to turn turn and shoot and everything works fine. When in mobile mode and single joystick are used controls player goes forward and slides sideways and backwards but doesn't turn to go backwards or turn to go sideways facing the direction the gun/camera is pointing. So he has to slide backwards to get on the other side of the enemy to shoot. The code from the player script is below
void Update ()
{
//dont do anything if game paused
if (GameManager.instance.gamePaused == true)
return;
Move();
if (Input.GetButtonDown("Jump"))
TryJump();
if(CrossPlatformInputManager.GetButton("Fire1"))
{
if (weapon.CanShoot())
weapon.Shoot();
}
CamLook();
}
// move horizontally based on movement inputs
void Move ()
{
// get the x and z inputs
float x = CrossPlatformInputManager.GetAxis("Horizontal") * moveSpeed;
float z = CrossPlatformInputManager.GetAxis("Vertical") * moveSpeed;
Vector3 dir = transform.right * x + transform.forward * z;
dir.y = rig.velocity.y;
//apply the velocity
rig.velocity = dir; ;
}
void CamLook()
{
float y = CrossPlatformInputManager.GetAxis("Mouse X") * lookSensitivity;
rotX += CrossPlatformInputManager.GetAxis("Mouse Y") * lookSensitivity;
//Clamp the vertical rotation
rotX = Mathf.Clamp(rotX, minLookX, maxLookX);
//rotate the camera and the player
cam.transform.localRotation = Quaternion.Euler(-rotX, 0, 0);
transform.eulerAngles += Vector3.up * y;
}
Your answer
Follow this Question
Related Questions
Using the IOS Keyboard 2 Answers
Mobile Keyboard event calls 2 Answers
TMP InputField does not fire onTouchScreenKeyboardStatusChanged event when keyboard becomes visible 0 Answers
Detect When GUI.Text Field gets focus. 0 Answers
How do you test a mobile game? 0 Answers