Click here to Skip to main content
15,868,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to build a first person mobile game and i need some assistance.

I am having some issues with my mobile joysticks. I have 2 joysticks, one for movement and one for the camera. The camera joysticks is not working, and the movement joystick only does something to the degree of a one step movement and moves the player in the opposite direction of the intended direction.

Here is the movement and camera script all in one (which may be one of the issues, not sure)

using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     [AddComponentMenu("Camera-Control/Smooth Mouse Look")]
     
     public class PlayerController : MonoBehaviour
     {
         public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
         public RotationAxes axes = RotationAxes.MouseXAndY;
         public float sensitivityX = 15F;
         public float sensitivityY = 15F;
         public float minimumX = -360F;
         public float maximumX = 360F;
         public float minimumY = -60F;
         public float maximumY = 60F;
         float rotationX = 0F;
         float rotationY = 0F;
         private List<float> rotArrayX = new List<float>();
         float rotAverageX = 0F;
         private List<float> rotArrayY = new List<float>();
         float rotAverageY = 0F;
         public float frameCounter = 20;
         Quaternion originalRotation;
         public bool controllable = false;
         public float speed = 7.0f;
         public float jumpSpeed = 6.0f;
         public float gravity = 20.0f;
         private Vector3 moveDirection = Vector3.zero;
         private CharacterController controller;
         public VirtualJoystick moveJoystick;
         public VirtualJoystick cameraJoystick;
         void Update()
         {
     
     
             if(moveJoystick.InputDirection != Vector3.zero)
             {
                 moveDirection = moveJoystick.InputDirection;
             }
             if (moveJoystick.InputDirection != Vector3.zero)
             {
                 moveDirection = cameraJoystick.InputDirection;
             }
             if (controller.isGrounded && controllable)
     
             {
     
                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     
                 moveDirection = transform.TransformDirection(moveDirection);
     
                 moveDirection *= speed;
     
     
     
                 if (Input.GetButton("Jump"))
     
                     moveDirection.y = jumpSpeed;
     
     
     
             }
     
             moveDirection.y -= gravity * Time.deltaTime;
     
             controller.Move(moveDirection * Time.deltaTime);
     
             if (axes == RotationAxes.MouseXAndY)
             {
                 rotAverageY = 0f;
                 rotAverageX = 0f;
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                 rotArrayY.Add(rotationY);
                 rotArrayX.Add(rotationX);
     
                 if (rotArrayY.Count >= frameCounter)
                 {
                     rotArrayY.RemoveAt(0);
                 }
     
                 if (rotArrayX.Count >= frameCounter)
                 {
                     rotArrayX.RemoveAt(0);
                 }
                 for (int j = 0; j < rotArrayY.Count; j++)
                 {
                     rotAverageY += rotArrayY[j];
                 }
     
                 for (int i = 0; i < rotArrayX.Count; i++)
                 {
                     rotAverageX += rotArrayX[i];
                 }
                 rotAverageY /= rotArrayY.Count;
                 rotAverageX /= rotArrayX.Count;
                 rotAverageY = ClampAngle(rotAverageY, minimumY, maximumY);
                 rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX);
                 Quaternion yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
                 Quaternion xQuaternion = Quaternion.AngleAxis(rotAverageX, Vector3.up);
                 transform.localRotation = originalRotation * xQuaternion * yQuaternion;
             }
             else if (axes == RotationAxes.MouseX)
             {
                 rotAverageX = 0f;
                 rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                 rotArrayX.Add(rotationX);
                 if (rotArrayX.Count >= frameCounter)
                 {
                     rotArrayX.RemoveAt(0);
                 }
     
                 for (int i = 0; i < rotArrayX.Count; i++)
                 {
                     rotAverageX += rotArrayX[i];
                 }
     
                 rotAverageX /= rotArrayX.Count;
                 rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX);
                 Quaternion xQuaternion = Quaternion.AngleAxis(rotAverageX, Vector3.up);
                 transform.localRotation = originalRotation * xQuaternion;
             }
             else
             {
                 rotAverageY = 0f;
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotArrayY.Add(rotationY);
                 if (rotArrayY.Count >= frameCounter)
                 {
                     rotArrayY.RemoveAt(0);
                 }
                 for (int j = 0; j < rotArrayY.Count; j++)
                 {
                     rotAverageY += rotArrayY[j];
                 }
                 rotAverageY /= rotArrayY.Count;
                 rotAverageY = ClampAngle(rotAverageY, minimumY, maximumY);
                 Quaternion yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
                 transform.localRotation = originalRotation * yQuaternion;
             }
         }
     
         void Start()
         {
             controller = GetComponent<CharacterController>();
             Rigidbody rb = GetComponent<Rigidbody>();
             if (rb)
                 rb.freezeRotation = true;
             originalRotation = transform.localRotation;
         }
     
         public static float ClampAngle(float angle, float min, float max)
         {
             angle = angle % 360;
             if ((angle >= -360F) && (angle <= 360F))
             {
                 if (angle < -360F)
                 {
                     angle += 360F;
                 }
                 if (angle > 360F)
                 {
                     angle -= 360F;
                 }
             }
             return Mathf.Clamp(angle, min, max);
         }
     }


Here is the Joystick script I have been using for the movement of the player

C#
using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     using UnityEngine.EventSystems;
     
     public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
     {
     
         private Image bgImg;
         private Image joystickImg;
     
         public Vector3 InputDirection { set; get; }
     
         private void Start()
         {
             bgImg = GetComponent<Image>();
             joystickImg = transform.GetChild(0).GetComponent<Image>();
             InputDirection = Vector3.zero;
         }
     
         public virtual void OnDrag(PointerEventData ped)
         {
             Vector2 pos = Vector2.zero;
             if(RectTransformUtility.ScreenPointToLocalPointInRectangle
                 (bgImg.rectTransform,
                     ped.position,
                     ped.pressEventCamera,
                     out pos))
             {
                 pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
                 pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
     
                 float x = (bgImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
                 float y = (bgImg.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
     
                 InputDirection = new Vector3(x, 0, y);
     
                 InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
     
                 joystickImg.rectTransform.anchoredPosition =
                     new Vector3(InputDirection.x * (bgImg.rectTransform.sizeDelta.x / 3)
                         , InputDirection.z * (bgImg.rectTransform.sizeDelta.y / 3));
             }
         }
         
         public virtual void OnPointerDown(PointerEventData ped)
         {
             OnDrag(ped);
         }
     
         public virtual void OnPointerUp(PointerEventData ped)
         {
             InputDirection = Vector3.zero;
             joystickImg.rectTransform.anchoredPosition = Vector3.zero;
         }
     }


and here is the camera joystick script

C#
using UnityEngine;
     using UnityEngine.UI;
     using UnityEngine.EventSystems;
     using System.Collections;
     
     public class CameraJoystick : MonoBehaviour
     {
     
         public VirtualJoystick joycam;
         private float distance = 10.0f;
         private float currentX = 0.0f;
         private float currentY = 0.0f;
         private float sensitivityX = 3.0f;
         private float sensitivityY = 1.0f;
     
     
         private void Update()
         {
             currentX += joycam.InputDirection.x * sensitivityX;
             currentY += joycam.InputDirection.z * sensitivityY;
         }
     
         private void LateUpdate()
         {
             Vector3 dir = new Vector3(0, 0, -distance);
             Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
         }
     }


so, what i want is the player to be able to use the joysticks to move the player and move the camera. I cannot figure out what i have done wrong, i need some code assistance.

thank you for any help.

What I have tried:

I have also tired to use this script

using UnityEngine
 using System.Collections;
 using System.Collections.Generic;
 using UnityStandardAssets.CrossPlatformInput;
 public class PlayerMovement : MonoBehaviour 
 {
 public float moveSpeed = 2.0f;
 public float moveHorizontal = 2.0f;
 public float moveVertical = 2.0f;
 
 private void Update () 
   {      
       float moveHorizontal = Input.GetAxisRaw ("Horizontal");
       float moveHorizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
       float moveVertical = Input.GetAxisRaw ("Vertical");
       float moveVertical = CrossPlatformInputManager.GetAxis ("Vertical");
          if( moveHorizontal != 0.0f )
        {
       Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0); 
       transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(-movement), 0.15F);
        }
          if( moveVertical != 0.0f )
        {
       Vector3 movement = new Vector3(moveVertical, 0.0f, 0); 
       transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(-movement), 0.15F);
        }
   }
 }
Posted
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900