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 SneakingMilk · Dec 03, 2017 at 09:36 PM · scripting problemscript.scripting beginner

Move from A to B after recieving touch input

I have been trying to get an object to move from A to B in a set period of time after the player touches the screen. I've looked at various examples however whenever I try to add touch input it doesn't quite work properly and consecutive inputs after the first touch effect the movement. Does anyone know how I could achieve this?

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 CatFisting · Dec 03, 2017 at 10:17 PM

It's important whether you're using C# or JS, but in C# I'd do it a bit like this.

 public GameObject object;
 bool hasTouched = false;
 Vector3 A;
 Vector3 B;
 Vector3 distance;



 void Start(){
 A = [A VECTOR3 COORDINATES HERE]
 B = [B VECTOR3 COORDINATES HERE]
 distance = A - B; 
 }

 void Update(){


 if (Input.touchCount > 0 && hasTouched == false){
       //do your moving here with an IEnumerator loop

         hasTouched = true;
     }
 }


Comment
Add comment · Show 3 · 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 SneakingMilk · Dec 04, 2017 at 11:58 AM 0
Share

Thank you very much for responding with this code, this works well for receiving the touch input. However I tried adding the IEnumerator loop but couldn't get it to work as I am very new to scripting, would it be possible to give an example or link me to something that I could add to the code to make it work.

avatar image CatFisting SneakingMilk · Dec 04, 2017 at 02:10 PM 0
Share

If it works well I assume it's already moving as you want it but the ti$$anonymous$$g is off. An IEnumerator will work off of a WaitForSeconds method to know how many times to go before repeating. Here's an IEnumerator example.

 private IEnumerator example(float 
 time){
 //do something small once
 yield return new WaitForSeconds(time);

 }

Call it somewhere earlier in the script with StartCoroutine(example (.5f));

You could repeat it a certain number if times using a basic 'for loop'.

https://unity3d.com/learn/tutorials/topics/scripting/loops

avatar image SneakingMilk CatFisting · Dec 04, 2017 at 08:41 PM 0
Share

Thank you so much for the example. I have now managed to get the IEnumerator working and have implemented it into my game with the touch input and it all functions as intended.

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

128 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

Related Questions

Help me with this code proplem,,Almost finish my game please help this once 3 Answers

Instantiate an object on mouse/cursor position. 1 Answer

Do something 1 centimeter away from the box collider 0 Answers

Pong goal collision 3 Answers

Play animation only when a key is being held down 2 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