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 Hoorza · May 01, 2017 at 07:45 PM · movement scriptfollowmousepositionmove an object

Object heading towards mouse click/ touch position, script improvement.

Hi all,

Just so it is clear, I am quite new to the programming.

I am learning by creating my first game for android , it is a simple Breakthrough/ Arkanoid clone. I got my script for the paddle working ok. But what I am looking for is the improvement in the behavior. Paddle moves left and right as it should. What I can't get it to do is that second touch overrides first touch and paddle will change direction of the movement. For example: If I touch the screen on the left side of the paddle it will move to the left until the edge of the screen, but if I want it to start moving to the right I need to release the finger before I touch the screen on the right side of the paddle. If I press it too early on the right, paddle will continue going to the left until I release the finger and press it again on the right side of the paddle.

P.S. I have used Input.GetMouseButtonDown as I am familiar with this method and I have read that will work on the phone(so far it does, tested with Unity Remote 5 and by uploading my app on the phone directly). Movement is simple left or right so at the moment I am not looking for new method unless you think Input.GetMouseButtonDown can generate some issues.

 public class PaddleMovement : MonoBehaviour
 {    
     public float speed = 1.5f;
     private Vector3 mouseClickPosition;
 
 
     void Start()
     {
         mouseClickPosition = transform.position;
     }
 
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             mouseClickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             mouseClickPosition.z = transform.position.z;
         }
         transform.position = Vector3.MoveTowards(transform.position, mouseClickPosition, speed * Time.deltaTime);
     }
 }

I want to understand it, not just copy the code if it is not to crazy I mean ;)

Thanks for help.

HoOrza

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 Andrea_Marchetti · May 18, 2017 at 06:49 PM

The Input.mousePosition is not designed for mobiles. What it probably does on mobile is that it takes the average point between all the points you are touching on the screen. Furthermore, but I am not sure, on iOS it doesn't work as well as on Android.

For mobile development use instead Input.GetTouch. It should be also faster too. If you want your character to follow the input of the last touch then you can check when Input.touchCount changes and allow your code to use the last item inserted into the array as the reference or you can check the TouchPhase of all touches and assign to a new variable the index of the one that has the phase equal to TouchPhase.Began.

Comment
Add comment · Show 1 · 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 Hoorza · May 24, 2017 at 08:29 PM 0
Share

Thanks @Andrea_$$anonymous$$archetti , I will look into it shortly when I start polishing my game. Once I got the right code and solution I will post it. I am sure there is a way to override first touch with second touch.

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

100 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

Related Questions

how to make your object move from point A to point B? 1 Answer

My bullet keeps following its spawn point after being instantiate. Pls help 1 Answer

How to spawn an object on button click and make it follow the mouse until left click? 0 Answers

How do I get a character to walk on walls and ceilings? 1 Answer

my box movements look jitter, how to fix it 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