• 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 /
avatar image
0
Question by itsmealex100 · Jul 01, 2015 at 04:10 PM · c#touchtouchpad

Move player along z axis on touch(touchPad)

Hi there,

Been trying this for days and cannot crack it. I want to move my player up and down the z axis, based on where the player is touching(clicking for PC) on the touchpad. I.e if the player is touching towards the top of the touchpad, the player moves up on the z axis, and if they are touching towards the bottom of the touchpad.... etc.

I am using Unity's 'Dual Touch Controls' Prefab, but I cannot get it to move the player on Touch. I have put the code for the touch pad below, any help would be appreciated.

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using UnitySampleAssets.CrossPlatformInput;
 
 
 [RequireComponent(typeof(Image))]
 public class TouchPad : MonoBehaviour , IPointerDownHandler , IPointerUpHandler
 {
 
     public enum AxisOption
     {                                                    // Options for which axes to use                                                     
         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 sensitivity = 1f;
 
     private Vector3 startPos;
     private Vector2 previousDelta;
     private Vector3 m_JoytickOutput;
     private bool useX;                                                          // Toggle for using the x axis
     private bool useY;                                                          // Toggle for using the Y axis
     private CrossPlatformInputManager.VirtualAxis horizontalVirtualAxis;               // Reference to the joystick in the cross platform input
     private CrossPlatformInputManager.VirtualAxis verticalVirtualAxis;                 // Reference to the joystick in the cross platform input
     private bool dragging = false;
     private int id =-1;
 
     //Others
     public GameObject player;
     private Vector3 destinationPosition;
     public Transform myTransform;
 
 
 #if !UNITY_EDITOR
     private Vector3 center;
     private Image image;
 #endif
     private Vector2 previousTouchPos;                                           // swipe style control touch 
 
 #if UNITY_EDITOR
     private Vector3 previousMouse;
 #endif
 
     void OnEnable()
     {
         CreateVirtualAxes();
 #if !UNITY_EDITOR
         image = GetComponent<Image>();
         center = image.transform.position;
 #endif
     }
 
 
     private void CreateVirtualAxes()
     {
         // set axes to use
         useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
 
         // create new axes based on axes to use
         if (useX)
             horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
         if (useY)
             verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
     }
 
     private void UpdateVirtualAxes(Vector3 value)
     {
 
         value = value.normalized;
         if (useX)
             horizontalVirtualAxis.Update(value.x);
 
         if (useY)
             verticalVirtualAxis.Update(value.y);
 
     }
 
     public void OnPointerDown(PointerEventData data)
     {
         Debug.Log ("TOUCHPAD WAS PRESSED ONCE");
         dragging = true;
         id = data.pointerId;
 
 
 #if !UNITY_EDITOR
         if (controlStyle != ControlStyle.Absolute )
             center = data.position;
 #endif
     }
 
     void Update () {
 
         if (!dragging) {
             return;
         }
     
     
 
         if (Input.touchCount >= id && id != -1) {
 
 
 
 #if !UNITY_EDITOR
 
             if (controlStyle == ControlStyle.Swipe)
             {
                 center = previousTouchPos;
                 previousTouchPos = Input.touches[id].position;
             }
             Vector2 pointerDelta = new Vector2(Input.touches[id].position.x - center.x , Input.touches[id].position.y - center.y).normalized * sensitivity;
 #else
 
             Vector2 pointerDelta;;
             pointerDelta.x = Input.mousePosition.x - previousMouse.x;
             pointerDelta.y = Input.mousePosition.y - previousMouse.y;
             previousMouse = new Vector3(Input.mousePosition.x , Input.mousePosition.y , 0f);
 #endif
             UpdateVirtualAxes (new Vector3 (pointerDelta.x, pointerDelta.y, 0));
         }
         Debug.Log ("TOUCHPAD IS BEING PRESSED");
 
             
 
     }
 
 
     public void OnPointerUp(PointerEventData data) {
         dragging = false;
         id = -1;
         UpdateVirtualAxes(Vector3.zero);
 
     }
 
 
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by itsmealex100 · Jul 01, 2015 at 04:36 PM

So I've Managed to solve this problem for the time being using the following:

 Debug.Log ("TOUCHPAD IS BEING PRESSED");
         Vector2 MousePos = Input.mousePosition;
         if (MousePos.y > 333) {
             player.transform.Translate (Vector3.forward * Time.deltaTime, Space.World);
         } else {
             player.transform.Translate (-Vector3.forward * Time.deltaTime, Space.World);
         }


This seems to work fine, although I am concerned that they might be a more efficient way to do this, especially as this is for a mobile game. Any ideas or reassurance that this is correct would still be appreciated!

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Touch - drag different objects 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rotate with two fingers 0 Answers

Script when i touch an object it must do something 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