Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Leroterr · Feb 11, 2015 at 03:01 AM · 2dtransformpositionvector3touch

How to make your object follow your touch position faster or instantaneously???

In my 2D game, the object doesn't instantly catch up to where my finger is. Yes, it makes it follow my touch much smoother while moving, but I want it to be instant, I want my 2D object to be instantly on where my finger is...

I also lose control of my object since when it gets late behind following my finger, my finger is no longer touching the collider, and I also want it to be able to only move and follow my finger when I'm touching its collider.

Here's the code I'm using right now. It follows my finger smoothly, but it can't catch up to my touch position to the point where I'm no longer touching its box collider:

 if(Physics.Raycast(ray, Mathf.Infinity, playerLayerMask))
     {
 
         transform.position = Camera.main.ScreenToWorldPoint
         (new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 9)); 
 
     }
Comment
Add comment · Show 3
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 kevinspawner · Feb 11, 2015 at 03:13 AM 0
Share

Did you tried with void fixed update?

avatar image Leroterr · Feb 11, 2015 at 03:20 AM 0
Share

@kevinspawner Yeah, my object still couldn't catch up to my finger when I'm trying to move it fast.

avatar image Glurth · Feb 11, 2015 at 08:03 PM 0
Share

perhaps using "touches" ins$$anonymous$$d of GetTouch will help: http://docs.unity3d.com/ScriptReference/Input-touches.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MaxEden · Feb 11, 2015 at 08:47 AM

Try to do this in LateUpdate()

Another solution is to introduce another variable

 Vector3 lastTouch;
 
 void LateUpdate(){
   if(Input.touches>0)
   {
     lastTouch = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 9));
   }
     transform.position = lastTouch;    
 }

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 Leroterr · Feb 11, 2015 at 09:27 AM 0
Share

Still works the same as the previous one.

avatar image
0

Answer by IvovdMarel · Feb 11, 2015 at 07:34 AM

Rather than continuously checking wether or not you are touching the object, you should set a boolean to true, similar to this:

 if (Input.GetMouseButtonDown(0)) 
 {
      if(Physics.Raycast(ray, Mathf.Infinity, playerLayerMask))
      {
            dragging = true;
      }
 }

and then set dragging to false when you release the mouse. (Input.GetMouseButtonUp(0))

While dragging, have the object follow your mouse as you did in your code.

Note - in case you're wondering, this also works for mobile touch.

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 Leroterr · Feb 11, 2015 at 08:24 AM 0
Share

I've tried that and it still works like my previous code. It gets left behind for some reason when I'm moving my finger too fast.

avatar image IvovdMarel · Feb 11, 2015 at 06:24 PM 0
Share

When you say left behind, you mean it stops following right? Not that it's just slow in following. Does your code look something like this?

 if (Input.Get$$anonymous$$ouseButtonDown(0)) 
  {
       if(Physics.Raycast(ray, $$anonymous$$athf.Infinity, playerLayer$$anonymous$$ask))
       {
             dragging = true;
       }
  }
 
 if (dragging) {
  transform.position = Camera.main.ScreenToWorldPoint (new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 9));
 
 }


avatar image tanoshimi · Feb 11, 2015 at 08:23 PM 0
Share

Using Get$$anonymous$$ouseButtonDown() for touch input is undocumented and likely unoptimised (and perhaps fragile). $$anonymous$$uch better to use Input.touches for touch input.

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

23 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

Related Questions

Lerp not working 1 Answer

Aim Down Sights Positioning Problem 1 Answer

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

Vector3.Slerp not working 2 Answers

Move with Rigidbody2D in the Z axis 3 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