Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FawkingAwesome · Jun 15, 2017 at 06:12 AM · androidunity 5gameandroid buildcross-platform

Changing to Mobile Platform

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;

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 = Input.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; using UnityEngine.UI;

namespace UnityStandardAssets.CrossPlatformInput { [RequireComponent(typeof(Image))] public class TouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { // Options for which axes to use public enum AxisOption { Both, // Use both OnlyHorizontal, // Only horizontal OnlyVertical // Only vertical }

     public enum ControlStyle
     {
         Absolute, // operates from teh center of the image
         Relative, // operates from the center of the initial touch
         Swipe, // swipe to touch touch no maintained center
     }


     public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
     public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
     public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
     public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
     public float Xsensitivity = 1f;
     public float Ysensitivity = 1f;

     Vector3 m_StartPos;
     Vector2 m_PreviousDelta;
     Vector3 m_JoytickOutput;
     bool m_UseX; // Toggle for using the x axis
     bool m_UseY; // Toggle for using the Y axis
     CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
     CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
     bool m_Dragging;
     int m_Id = -1;
     Vector2 m_PreviousTouchPos; // swipe style control touch


if !UNITY_EDITOR

private Vector3 m_Center; private Image m_Image; #else Vector3 m_PreviousMouse; #endif

     void OnEnable()
     {
         CreateVirtualAxes();
     }

     void Start()
     {

if !UNITY_EDITOR

        m_Image = GetComponent<Image>();
         m_Center = m_Image.transform.position;

endif

    }

     void CreateVirtualAxes()
     {
         // set axes to use
         m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

         // create new axes based on axes to use
         if (m_UseX)
         {
             m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
         }
         if (m_UseY)
         {
             m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
         }
     }

     void UpdateVirtualAxes(Vector3 value)
     {
         value = value.normalized;
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Update(value.x);
         }

         if (m_UseY)
         {
             m_VerticalVirtualAxis.Update(value.y);
         }
     }


     public void OnPointerDown(PointerEventData data)
     {
         m_Dragging = true;
         m_Id = data.pointerId;

if !UNITY_EDITOR

    if (controlStyle != ControlStyle.Absolute )
         m_Center = data.position;

endif

    }

     void Update()
     {
         if (!m_Dragging)
         {
             return;
         }
         if (Input.touchCount >= m_Id + 1 && m_Id != -1)
         {

if !UNITY_EDITOR

         if (controlStyle == ControlStyle.Swipe)
         {
             m_Center = m_PreviousTouchPos;
             m_PreviousTouchPos = Input.touches[m_Id].position;
         }
         Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
         pointerDelta.x *= Xsensitivity;
         pointerDelta.y *= Ysensitivity;

else

            Vector2 pointerDelta;
             pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
             pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
             m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);

endif

            UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
         }
     }


     public void OnPointerUp(PointerEventData data)
     {
         m_Dragging = false;
         m_Id = -1;
         UpdateVirtualAxes(Vector3.zero);
     }

     void OnDisable()
     {
         if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
             CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);

         if (CrossPlatformInputManager.AxisExists(verticalAxisName))
             CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
     }
 }

} How would I control the movement with an Android screen? Thanks guys I appreciate any help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(DISSOLVED) Error : Failed to build apk. (Unity 2017.2.0f3) 2 Answers

"No Android Device Found" on Build and Run 1 Answer

Unable to merge android manifest Android? 0 Answers

Unity Remote 5 Not Connecting 3 Answers

I have a few questions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges