Search Unity

Make a marble move for Android platform

Discussion in 'Scripting' started by GameForLife, Nov 1, 2014.

  1. GameForLife

    GameForLife

    Joined:
    Aug 17, 2013
    Posts:
    41
    Please help. I am able to make my marble move for the Mac, Windows, Web Player platforms. However, I cannot get my joystick to work in Android. For instance, my joystick moves around. However, it will not pick up the rigidbody.Addforce for my sphere. Only my joysticks and keyboards.

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class PlayerControlNew : MonoBehaviour
    5.     {
    6.      
    7.         public float speed;
    8.         public float speedArc = 10;
    9.         public GameObject moveJoy;
    10.         //accelerometer
    11.         private Vector3 zeroArc;
    12.         private Vector3 curAc;
    13.         private float sensH = 10;
    14.         private float sensV = 10;
    15.         private float smooth = 0.5f;
    16.         public Vector3 movement;
    17.         public float moveSpeed = 6.0f;
    18.     //    public  Joystick joystickRight;
    19.      
    20.         void Start()
    21.         {
    22. //                    ResetAxes ();
    23.                     moveJoy = GameObject.Find("LeftJoystick");
    24.     }
    25.      
    26.      
    27.      
    28.         void FixedUpdate()
    29.         {
    30.                 if (SystemInfo.deviceType == DeviceType.Desktop) {
    31.             float moveHorizontal = Input.GetAxisRaw ("Horizontal");
    32.             float moveVertical = Input.GetAxisRaw ("Vertical");
    33.          
    34.                             Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
    35.          
    36.                             rigidbody.AddForce (movement * speed * Time.deltaTime);
    37.                     } else {
    38.                             //Android controls
    39.  
    40.  
    41.                     }
    42.          
    43.         }
    44. //
    45. //        void ResetAxes()
    46. //        {
    47. //            zeroArc = Input.acceleration;
    48. //            curAc = Vector3.zero;
    49. //        }
    50.  
    51.     void Update ()
    52.     {
    53.                 Vector3 forward = Camera.main.transform.TransformDirection (Vector3.forward);
    54.                 forward.y = 0;
    55.                 forward = forward.normalized;
    56.  
    57.  
    58.                 Vector3 forwardForce = new Vector3 ();
    59.                 if (Application.platform == RuntimePlatform.Android) {
    60.                         float tmpSpeed = moveJoy.GetComponent<LeftJoystick> ().position.y;
    61.                         forwardForce = forward * tmpSpeed * 1f * moveSpeed;
    62.                 } else {
    63.                         forwardForce = forward * Input.GetAxis ("Vertical") * moveSpeed;
    64.      
    65.      
    66.                 }
    67.                 rigidbody.AddForce (forwardForce);
    68.  
    69.  
    70.                 Vector3 right = Camera.main.transform.TransformDirection (Vector3.right);
    71.                 right.y = 0;
    72.                 right = right.normalized;
    73.  
    74.                 Vector3 rightForce = new Vector3 ();
    75.                 if (Application.platform == RuntimePlatform.Android) {
    76.                         float tmpSpeed = moveJoy.GetComponent<Joystick> ().position.x;
    77.                         rightForce = right * tmpSpeed * 0.8f * moveSpeed;
    78.                 } else {
    79.                         rightForce = right * Input.GetAxis ("Horizontal") * moveSpeed;
    80.                 }  
    81.                 rigidbody.AddForce (rightForce);
    82.         }
    83.  
    84. }
    85.  
    86.  
    87.  
    upload_2014-10-31_23-10-15.png
     
    Last edited: Nov 1, 2014
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Assuming you want to use the acceleration of the device, try this: link!

    Of course if you wanted to do something with touch instead, I recommend making buttons that you can press when on mobile.
     
  3. GameForLife

    GameForLife

    Joined:
    Aug 17, 2013
    Posts:
    41
     
  4. GameForLife

    GameForLife

    Joined:
    Aug 17, 2013
    Posts:
    41
    Thanks. I see that the Joystick and position parameters are in red. Therefore, it cannot locate it in context. Thank You.