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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by bloggerbeg · Jan 29, 2014 at 03:43 PM · c#input

I'm unable to implement touch control in unity for my c# script, what's wrong with this code, not working

Look i'm total noob in unity scripting, could you please tell me what's wrong with this code, i'm not able to implement the touch, i want when user taps on the screen, to store that x and y coordinate and move the object to that location, i'm pasting the code, please tell me how to implement it

 void controlTouch()
 {
     float x,z;
     Vector3 mousePos = Input.mousePosition;
     mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
     targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x, 0, transform.position.z));
     transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
     if(Input.touches.Length > 0)
     {
         //if(Input.touches[0].phase == TouchPhase.Moved)
         //{
             x = Input.GetTouch(0).deltaPosition.x;
             z = Input.GetTouch(0).deltaPosition.y;
             //x = Input.touches[0].deltaPosition.x * runSpeed * Time.deltaTime;
             //z = Input.touches[0].deltaPosition.y * runSpeed * Time.deltaTime;
         //}
         /*else
         {
             x = 0.0f;
             z = 0.0f;
         }*/
     }
     else
     {
         x = 0.0f;
         z = 0.0f;
     }
 
     Vector3 input = new Vector3(x,0,z);
     currentVelocityModifer = Vector3.MoveTowards(currentVelocityModifer,input,acceleration * Time.deltaTime);
     Vector3 motion = currentVelocityModifer;
     motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? 0.7f : 1;
     motion *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
     motion += Vector3.up * -8;
 
     controller.Move(motion * Time.deltaTime);
 
 }
Comment
Add comment · Show 1
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 VildNinja · Jan 29, 2014 at 05:20 PM 0
Share

deltaPosition is not the position of the touch, but the relative position compared to last frame, in other words how far the finger have moved.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Maui-M · Jan 29, 2014 at 07:35 PM

Delta position is the change in the position since the last input update. If you want to get the position of the touch on the screen use the following:

Your code:

 x = Input.GetTouch(0).deltaPosition.x;
 z = Input.GetTouch(0).deltaPosition.y;


Should be:

 x = Input.GetTouch(0).position.x;
 z = Input.GetTouch(0).position.y;


If you want to move the object to that position that is another issue. You will have to subtract the objects position from the touch position. That will tell you which direction you need to move the object.

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Multiple Cars not working 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How to keep my cursor inside window boundaries 0 Answers

Distribute terrain in zones 3 Answers

Animation won't complete unless button is held 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