• 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 TheSaviour · May 24, 2016 at 02:50 PM · touchmousepositionmouseclicktouchphasetouchcount

How do I make a rigid body move towards position of touch

I want a sphere to only move either left or right (using rigidbody.addforce) depending on where the screen has been touched. I used the following code but the sphere moves very slowly.

 if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Vector3 touchPosition = Input.GetTouch(0).position;
             double halfScreen = Screen.width / 2.0;
 
             if(touchPosition.x < halfScreen)
             {
                 rigidbody.AddForce(Vector3.left * speed * Time.deltaTime);
             } 
             else if (touchPosition.x > halfScreen) 
             {
                 rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
             }
 
         }

This code was the solution from another thread where the OP was having the same problem. But it's not working for me. I'm trying to develop for the android platform. Are there any alternatives? Is there a way I can re-write the code using mouse click functions (so that android converts it to touch after the game is built)?

Comment
Add comment · Show 6
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 Mmmpies · May 24, 2016 at 03:45 PM 0
Share

What's speed set to? Or more to the point set it to higher.

Really though you'll likely want it to stop when it gets there so maybe use move towards ins$$anonymous$$d.

avatar image TheSaviour · May 24, 2016 at 04:03 PM 0
Share

At first, the speed was set to 7. At that speed, the sphere only tilted a bit (I could tell because of the design I put on the sphere). So I set the speed to 20 just to see what happens. At that speed, the sphere moves REALLY slowly. I should let you know that I've been testing the game using keyboard controls for the past couple of weeks and the movement of the sphere was perfect. But now I'm thinking of switching to touchscreen inputs because I want to release it for Android. This is the code I've been using on the sphere for keyboard inputs:

 float horizontal = Input.GetAxis ("Horizontal");
         Vector3 movement = new Vector3 (horizontal, 0.0f, 0.0f);
         rigidbody.AddForce (movement * speed);

The sphere moves fine with the Left and Right keyboard buttons when I use this code. But now, I want to use touch screen inputs ins$$anonymous$$d.

avatar image ricke44654 TheSaviour · May 24, 2016 at 04:12 PM 0
Share

@TheSaviour - After checking your touch code vs. your keyboard code, I noticed the touch code is adding force using Time.deltaTime in the calculation, while your keyboard code is not using it. This could account for your speed difference, ya? :)

avatar image Mmmpies · May 24, 2016 at 04:35 PM 0
Share

Yep Time.DeltaTime is (or should be) in fractions as it's 1 second divided by the current frames per second and multiply by a fraction is like divide by a whole number. It's not a bad thing to do though as it means the force will move the sphere X distance in X seconds on all platforms regardless of frames per second.

You can just up the force you apply but I really think you'd be better using move towards as adding the exact amount of force to move sphere exactly to a point is going to be a lot harder.

avatar image TheSaviour · May 25, 2016 at 05:30 PM 0
Share

How can I use $$anonymous$$oveTowards in this script? I can't find any info on implementing Vector3.$$anonymous$$oveTowards on a rigid body as a reaction to touch input...

avatar image Mmmpies TheSaviour · May 25, 2016 at 08:14 PM 0
Share

Do you specifically need the rigidbody or are you just using that so you can add force?

If the former then try something like this...

 rigidbody.AddForce((target.position - transform.position) * 2000 * Time.deltaTime);

Or try using rigidbody.$$anonymous$$ovePosition.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

TouchPhase.Began hold problem 0 Answers

How to convert Input.GetAxis(MouseAxis.MOUSE_Y), Input.GetAxis(MouseAxis.MOUSE_X) for touch 0 Answers

Hello I have a problem with touches in Unity 2020.1 0 Answers

Sprite occasionally moving to left when dragging to right 0 Answers

2D touch - Why is this movement so jittery? 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