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 lol2dubs · Dec 29, 2016 at 05:54 AM · c#androidmovement

[Android] Character movement script help

Finding exactly what I need has proven difficult. I have a working script, but it doesn't function exactly the way I want it to.

Desired Functionality:

Character moves to the user's finger and "floats" past a bit, as if floating in space with little to no gravity. After moving past the user's finger, the character slowly moves back to the finger location. The character follows the user's finger at all times, as long as the finger is on the screen. Following should be precise.

Production Functionality:

Character moves towards user's finger, but does not stop, nor turn back towards the finger. Character continues moving in general direction of the finger and does not stop until it slams into the side of the screen. Once it has hit the side of the screen, it takes a moment to get the character to follow the finger again. Following is not precise, and seems to be off by some degree. The character will fly towards the stationary finger, but it, more often than not, will be off on the y axis by a few degrees.

 // BEGIN MOVEMENT VARIABLE SECTION
     public float speed = .06f;
     Vector2 currentPos;
     float xmin;
     float xmax;
     float ymin;
     float ymax;
     private Vector3 finger;
     private Transform myTrans, camTrans;
     Rigidbody2D rb;
     float force;
     // END MOVEMENT VARIABLE SECTION
 
 
 
     void Update ()
         {
     
             print (Input.GetTouch (0).phase + " touchPhase");
             //TOUCH INPUT CONTROLLER
             if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
                 // begin animation and movement scripting
                 Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
                 finger = Camera.main.ScreenToWorldPoint (tempTouch);
                 //myTrans.LookAt (finger);
                 myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
                 rb.AddForce (transform.forward * force);
                 //print (myTrans.transform.position + " mytrans");
                 //print (finger + " finger"); 
             }
     
             if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
                 // begin animation and movement scripting
                 Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
                 finger = Camera.main.ScreenToWorldPoint (tempTouch);
                 //myTrans.LookAt (finger);
                 myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
                 rb.AddForce (transform.forward * force);
                 //print (myTrans.transform.position + " mytrans");
                 //print (finger + " finger"); 
             }
     
             if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
                 // begin animation and movement scripting
                 Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
                 finger = Camera.main.ScreenToWorldPoint (tempTouch);
                 //myTrans.LookAt (finger);
                 myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
                 rb.AddForce (transform.forward * force);
                 //print (myTrans.transform.position + " mytrans");
                 //print (finger + " finger"); 
             }
             //TOUCH INPUT CONTROLLER
     
             //CLAMP TO SCREEN CONTROLLER
             float distance = transform.position.z - Camera.main.transform.position.z;
             Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
             Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance));
             xmin = leftmost.x + .5f;
             xmax = rightmost.x - .5f;
             Vector3 topmost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
             Vector3 bottommost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, distance));
             ymin = topmost.y + .5f;
             ymax = bottommost.y - .5f;
             // restrict the player to the gamespace
             float newX = Mathf.Clamp (transform.position.x, xmin + .25f, xmax - .25f);
             float newY = Mathf.Clamp (transform.position.y, ymin + .5f, ymax - .5f); 
             transform.position = new Vector3 (newX, newY, transform.position.z);
             //CLAMP TO SCREEN CONTROLLER
     
     }
 

The clamp is working fine, but I figured I'd include it in case you needed it.

I've pieced this code together over hours and hours of video and Unity answers and Stack Exchange scouring.

Any input would be greatly appreciated.

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
Best Answer

Answer by lol2dubs · Dec 30, 2016 at 06:40 PM

I've figured this out for anyone who is interested. It's a much more elegant solution, as well.

         //STARSPEED CONTROLLER 
 
         if (starCollide == true) {
             speed = 12.0f;
         } else { 
             speed = 5.5f;
         }
 
         //STARSPEED CONTROLLER 
 
         //TOUCH INPUT CONTROLLER
         if (transform.position != realPos) {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, realPos, step);
         }
          
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began || Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (0).phase == TouchPhase.Stationary) {            
             Vector3 fingerPos = Input.GetTouch (0).position; 
             tempPos = fingerPos;
             tempPos.z = 0;
             realPos = Camera.main.ScreenToWorldPoint (tempPos);
             realPos.z = 0;
             rb.AddForce (transform.forward * force);
         }
 
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
             realPos = transform.position;
         }
         //TOUCH INPUT CONTROLLER
Comment
Add comment · 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

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

311 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 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 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

Switch PC Controls to Android ? 1 Answer

Buttons for Player movement 0 Answers

Player follow touch when holding down your finger on the screen (C#) 1 Answer

Unity2d-Android-C#: How to convert current movement script to be used for touchscreen? 1 Answer

Having trouble fading in/out animation on Android 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