Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by NDPW · Feb 22, 2015 at 09:10 PM · javascriptmovementpositionobjecttouchscreen

2D get touch input

Hello fellow unity developers, I am stuck with a little problem here, the problem is that I don't seem to get any touch input, the purpose of this script is to detect of the position of the player is higher/lower on the x/y axes and then follow an instruction. Determining the player position does work but any touch input doesn't seem to get picked up, Thanks in advance!

This is the script:

 function PlayerMove() {
 
 var pos = Camera.main.ScreenToWorldPoint(Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
 var playerposx = GameObject.Find("player2").transform.position.x;
 var playerposy = GameObject.Find("player2").transform.position.y;
 
 //Debug.Log (playerposx); //display playerpos x in console
 //Debug.Log (playerposy); //display playerpos z in console
 
     if (pos.x < playerposx){ // move to the left
          
          rigidbody2D.velocity.x = speed * -1;
          
         }
      
         else {
          
          Debug.Log ("Move to the left doesn't work");
              
            }
     }
Comment
Add comment · Show 2
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
avatar image slavo · Feb 23, 2015 at 09:47 AM 0
Share

Are you calling Player$$anonymous$$ove from Update function?

avatar image NDPW · Feb 23, 2015 at 09:50 AM 0
Share

Yes I am, so that should be working

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skylem · Feb 23, 2015 at 10:26 AM

i believe the issue might be that your defining the variable but not actually initiating anything to determine if you have hit the screen i could be wrong but Try this to check

 // to set the pos variable when we hit the screen.
 if(Input.GetTouch(0)) { 
 var pos = Camera.main.ScreenToWorldPoint(Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
 Debug.Log("pos.ToString()); // this will tell u if the variable
 is atleast recieving its correct value
 }
 

hope this helps, if it doesn't let me know and i'll re-assess.

Comment
Add comment · Show 5 · 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
avatar image NDPW · Feb 23, 2015 at 11:16 AM 0
Share

Thanks for the help, unfortunately the code doesn't work, the "Input.GetTouch" can't be called in this script (BCE0026: 'UnityEngine.Touch' cannot be used in a boolean context.)

So I gave it a try it with "if(Input.touchCount > 0)" but this didn't detect any input at all.

avatar image skylem · Feb 23, 2015 at 11:30 AM 0
Share

ah appologies i assumed that the touch value was a boolean i'll do some playing around and see if i can't deter$$anonymous$$e the type and get back to you.

avatar image skylem · Feb 23, 2015 at 11:40 AM 0
Share

there appears to be quite a few values regarding Touch i believe this is what your looking for.

 public Touch tuch;
 
 if(tuch.phase == TouchPhase.Stationary) {
     Debug.Log("is this working?!?!?!");
 }

there is also TouchPhase.Canceled, TouchPhase.Began, TouchPhase.Ended TouchPhase.$$anonymous$$oved

if none of this helps let me know

avatar image NDPW · Feb 24, 2015 at 11:53 AM 0
Share

what do you mean with

 public Touch tuch;

I assume you meant something like this

 var touch:Touch;

both of these are not working.

avatar image skylem · Feb 26, 2015 at 01:03 AM 0
Share

sorry for the delay, it could be that javascript operates differently sorry if this isn't working for you i don't have any alternative suggestions, as i said before i know very little about touchscreen scripting.

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

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

Camera Scripting Help (Sims Style) 0 Answers

How to get an Objects position per axis? 1 Answer

Translate Craig Reynolds's steering behaviors 0 Answers

Moving an object 0 Answers

Saving Object Position in Float NOT Object 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