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 /
avatar image
0
Question by kong04601 · Mar 28, 2017 at 07:08 AM · androidtouch controls

Help with touch input.

Hi, I'm kinda new to unity and I am playing around with touch controls. I am making a sort of space shooting game where the player controls the character with touch input. The touch is for moving the character around. my problems is that, when I tap to a space in the screen, the character jumps onto that area that I have tapped.

here is a sample of the game : https://developer.cloud.unity3d.com/share/Wk9HwJPTMG/

Also I have made a fire button, where the player should press that in order to fire, the problem is my character jumps to where the fire button is.

Is there a way to restrict the touch to not jump around whenever the player touch other spaces of the screen?

  • See attached Codes and SS below.

Thank you!

Here is my code :

 if (getInput())
         {
             Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
             float x = Mathf.Clamp(rawPosition.x, -2.81f, 2.81f);
             float y = Mathf.Clamp(rawPosition.y, -4.5f, Camera.main.orthographicSize);
             Vector3 targetPosition = new Vector3(x, y, 0);
             GetComponent<Rigidbody2D>().MovePosition(targetPosition*Time.deltaTime*40f);

         //Instantiate(bullet, transform.position, Quaternion.identity);
     }
     else
     {
         
         Vector2 pos = transform.position;

         pos.x += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         pos.y += Input.GetAxis("Vertical") * speed * Time.deltaTime;

         #region Position Check
         if (pos.y > Camera.main.orthographicSize)
         {
             pos.y = Camera.main.orthographicSize;
         }
         else if (pos.y <= -4.5f)
         {
             pos.y = -4.5f;
         }
         
         if(pos.x >= 2.81f)
         {
             pos.x = 2.81f;
         }
         else if( pos.x <= -2.81f)
         {
             pos.x = -2.81f;
         }
         #endregion

         transform.position = pos;
     }
 bool getInput()
     {
         bool fire = Input.GetButton("Fire1");
         bool touch = Input.touchCount > 0;
         return fire || touch;
     }


and here is how my game looks like alt text

ss.png (217.9 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Standard Asset joystick has issues when using custom resolution 1 Answer

Touch input broken with multi-touch 1 Answer

[SOLVED] Touch Controls Not Responsive 1 Answer

help with touch 2 Answers

Move object with touch 3D with script 0 Answers


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