0

Hey guys thanks in advance for helping me, I'm new to Unity and was doing a Captain Blaster tutorial. The only problem is I want to use the mobile platform instead of Windows. I've set the build setting to Android, and inserted Dual Touch controls. I deleted the control touch pad and inserted two moveTouchPads Side by side. I want you to move to the right when you tap the right side of the screen, and move to the left when you tap the left side of the screen. The code for the ship control is

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    namespace UnityStandardAssets.CrossPlatformInput
    {
    public class ShipControl : MonoBehaviour {
        public float playerSpeed = 10f;
        public GameControl gameController;
        public GameObject bulletPrefab;
        public float reloadTime = 0.5f;

        private float elapsedTime = 0;


        void Update ()
        {
            elapsedTime += Time.deltaTime;

            float xMovement = CrossPlatformInputManager.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;
            float xPosition = Mathf.Clamp (xMovement, -7f, 7f);
            transform.Translate (xPosition, 0f, 0f);

            if (Input.GetButtonDown ("Jump") && elapsedTime > reloadTime) {
                Vector3 spawnPos = transform.position;
                spawnPos += new Vector3 (0, 1.2f, 0);
                Instantiate (bulletPrefab, spawnPos, Quaternion.identity);
            }
        }
        void OnTriggerEnter2D (Collider2D other)
        {
                gameController.PlayerDied ();
        }
    }

}

The code for the Move Touch Pad is

using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UnityStandardAssets.CrossPlatformInput
{
    public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        // designed to work in a pair with another axis touch button
        // (typically with one having -1 and one having 1 axisValues)
        public string axisName = "Horizontal"; // The name of the axis
        public float axisValue = 1; // The axis that the value has
        public float responseSpeed = 3; // The speed at which the axis touch button responds
        public float returnToCentreSpeed = 3; // The speed at which the button will return to its centre

        AxisTouchButton m_PairedWith; // Which button this one is paired with
        CrossPlatformInputManager.VirtualAxis m_Axis; // A reference to the virtual axis as it is in the cross platform input

        void OnEnable()
        {
            if (!CrossPlatformInputManager.AxisExists(axisName))
            {
                // if the axis doesnt exist create a new one in cross platform input
                m_Axis = new CrossPlatformInputManager.VirtualAxis(axisName);
                CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);
            }
            else
            {
                m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);
            }
            FindPairedButton();
        }

        void FindPairedButton()
        {
            // find the other button witch which this button should be paired
            // (it should have the same axisName)
            var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];

            if (otherAxisButtons != null)
            {
                for (int i = 0; i < otherAxisButtons.Length; i++)
                {
                    if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this)
                    {
                        m_PairedWith = otherAxisButtons[i];
                    }
                }
            }
        }

        void OnDisable()
        {
            // The object is disabled so remove it from the cross platform input system
            m_Axis.Remove();
        }


        public void OnPointerDown(PointerEventData data)
        {
            if (m_PairedWith == null)
            {
                FindPairedButton();
            }
            // update the axis and record that the button has been pressed this frame
            m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));
        }


        public void OnPointerUp(PointerEventData data)
        {
            m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, 0, responseSpeed * Time.deltaTime));
        }
    }
}

How would I control the movement with an Android screen? Thanks guys I appreciate any help! I know the touch pad code might not be helpful, sorry I put it there just in case.

Jimmy Musser
  • 63
  • 2
  • 8
  • Find one of the Joystick prefabs from CrossPlatformInput/Prefabs .... Replace `Input.GetAxis ("Horizontal")` **with** `CrossPlatformInputManager.GetAxis("Horizontal")`. See answer from duplicated question if still confused. – Programmer Jun 15 '17 at 09:08
  • @Programmer I did this, however it just made it unplayable on both the computer and my phone (I couldn't move on my phone but the code seemed to work). How do I move from touch on the screen? Thanks! – Jimmy Musser Jun 16 '17 at 10:12
  • I would make sense to add EDIT in your question then add the new `ShipControl` script that doesn't work. I want to see it. – Programmer Jun 16 '17 at 10:14
  • @Programmer Yeah I put it in, thanks for helping me out. – Jimmy Musser Jun 16 '17 at 10:20
  • Don't just say it doesn't work. Does the virtual joystick move when moving it? Use `Debug.Log` to log the value of `CrossPlatformInputManager.GetAxis("Horizontal")` and then tell me if the value changes when the virtual joysctick is being moved. It's hard to help you without these. I will be out for few hours and will continue to help when I come back. – Programmer Jun 16 '17 at 10:32
  • @Programmer I was hoping to just use tapping and swiping the screen to move the player, I put the code I'm replacing the touch pad code in my question with what I put into my gameControl Object hoping it would work. Thanks – Jimmy Musser Jun 16 '17 at 22:20

0 Answers0