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
0
Question by Maribe1 · Aug 18, 2014 at 09:22 AM · touchfollowcontrol

Touch Follow 3d.

I’m struggling right now for the control of my game all I can see in the youtube and into the unity forum is touch follow of 2D. Help me I’m so depress for this control I can’t fix this. The 3d gameobject must follow my finger when I touch it in the screen.

 using UnityEngine;
 using System.Collections;
 public class TouchMove : MonoBehaviour {
 public float speed = 0.02f;
 // Use this for initialization
 void Start () {
 }
 // Update is called once per frame
 void Update () {
 if (Input.touchCount > 0) {
 // The screen has been touched so store the touch
 Touch touch = Input.GetTouch(0);
 if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
 // If the finger is on the screen, move the object smoothly to the touch position
 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,10));
 transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime);
 }
 }
 }
 }

Comment
Add comment · Show 2
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 robertbu · Aug 18, 2014 at 03:25 PM 0
Share

I see nothing wrong with this code. This code will cause your object to move towards the first finger in the touch array at the rate of 1 unit per second. What behavior are you getting, and how is it different than your desired behavior? What is your scale?

avatar image Maribe1 · Aug 18, 2014 at 03:54 PM 0
Share

yeah it's working perfectly in 2d, but when in 3d I touch the screen my character go up. I only want x z to move my character not x y. help me T_T

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Aug 18, 2014 at 05:13 PM

Aaaah, now I see your problem. In asking questions, you really need to spell out the issue since we don't see your action. The current code is moving the object in the 3D space. I'm assuming you have an angled camera with an object that is setting on a plane parallel to the XZ plane. The solution is to use a Raycast(). I like to use Unity's mathematical Plane class to solve this problem. Here is an untested rewrite of your code to use a the Plane class to solve your problem:

 using UnityEngine;
 using System.Collections;
 public class TouchMove : MonoBehaviour {
     public float speed = 0.02f;
 
     void Update () {
         if (Input.touchCount > 0) {
             // The screen has been touched so store the touch
             Touch touch = Input.GetTouch(0);
             if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
                 // If the finger is on the screen, move the object smoothly to the touch position
                 Plane plane = new Plane(Vector3.up, transform.position);
                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 float dist;
                 if (plane.Raycast (ray, out dist)) {
                     transform.position = ray.GetPoint (dist);
                 }
             }
         }
     }
 }

Comment
Add comment · Show 6 · 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 Maribe1 · Aug 18, 2014 at 05:37 PM 0
Share

I LOVE YOU $$anonymous$$AN !!!! THAN$$anonymous$$S FOR THIS <3

avatar image Maribe1 · Aug 20, 2014 at 04:45 PM 0
Share

there's a problem here man I thought it was good but when I test it and touch into the screen the character blink into that position.

avatar image robertbu · Aug 20, 2014 at 07:01 PM 0
Share

Yep, the code transports to that position. You did not specify otherwise in your question. If you want to do it over time, change lines 16 to:

   transform.position = Vector3.$$anonymous$$oveTowards(transform.position, ray.GetPoint(dist), Time.deltaTime * speed);

...where 'speed' is a variable you define or it can be replaced by a constant. 'speed' is units per second. Note that visually, when the object is further from the camera, it will appear to move slower.

avatar image Maribe1 · Sep 04, 2014 at 02:36 PM 0
Share

why my character always collide to my gameobjects even though it has a collider. :\

avatar image robertbu · Sep 06, 2014 at 02:03 AM 0
Share

@$$anonymous$$aribe1 - I don't understand your comment. This question and the code has nothing to do with collisions. $$anonymous$$aybe you want to open a new question with a full description of correct behavior and what is going wrong.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Touchinput script converted from 3d to 2d 1 Answer

I want to draw a line(curved) as finger moves on screen after that move a instantited gameobject/rigidbody following that path. 2D 0 Answers

Adding Touch Controls to Car Game 1 Answer

Move to touch direction 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