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 oars · Jul 29, 2017 at 04:40 AM · 2dtouchtouch controlsslingshot

Android 2D slingshot with position relative to where the user touched?

This is only my second project in Unity and my first one with touch controls so I'm a little unsure of how to move forward. I am creating a game where the player flings a character back and fort across the screen between two tethers by pulling back on him and releasing. I got it working by monitoring the users finger position and setting the character to it, with a few limitations to ensure it doesn't pull back too far or too low ect. But that required the user to move their finger back and forth to reach the character each time, where as for convinence I want them to be able to just swipe anywhere on the screen and the character to move relative to his position. I attempted to change my code to do this by recording the begin touch phase and subtracting it from the current touch position, then setting the characters position equal to that plus its original position. However this causes the character to instantly be pulled way too far back off the screen and becomes nonresponsive to any motion. they just sit there in the air until you release them. I'm not sure what's causing this behavior, any ideas of what it could be, or perhaps of a better way to accomplish what I want?

my code is as follows:

    void Dragged()
 {
     Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
     Vector2 anchortomouse = mousePoint - anchor.transform.position;
     Vector3 touchmove = mousePoint - touchOrigin;

     if(touchmove.sqrMagnitude > maxstretchsqr)
     {
         mouseRay.direction = touchmove;
         touchmove = mouseRay.GetPoint(maxstretch);
     }

     if(touchmove.y > anchor.position.y - .25f && ToIgnore.value == 2 )
     {
         touchmove.y = anchor.position.y - .25f;
     }

     if(touchmove.y < anchor.position.y + .25f && ToIgnore.value == 8)
     {
         touchmove.y = anchor.position.y + .25f;
     }

     if(touchmove.x > 2.4)
     {
         touchmove.x = 2.4f;
     }

     if(touchmove.x < -2.4)
     {
         touchmove.x = -2.4f;
     }

     mousePoint.z = 0;
     touchmove.z = 0;
     transform.position = anchor.transform.position + touchmove;
 }

where touchOrigin is the original touch position recorded earlier in the code. And anchor is the position of the anchor of the springjoint the character is attached to.

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

177 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

Related Questions

Simple cell-based game goes bonkers when clicked on the edge of the cell 0 Answers

Error : The second touch should not work at all. How do I fix this error?,Error: The second touch should not work at all. How do I fix this error? 0 Answers

Error CS1026: Unexpected symbol ;, expecting ) 0 Answers

Any one help me in this script? 0 Answers

Hi guys, this code controls my object with the use of a mouse to drag it, but I want to use a touch control in able to drag the object. Can anyone help me? Thanks 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