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 /
avatar image
0
Question by Easy_ · Oct 20, 2016 at 08:01 AM · touchhold

hold touch to move, tap to rotate

 public float holdTime = 4f;
 
 void Update()
     {
         Move();
     }
 
     public void Move()
     {
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             float startTime = Time.time;
             if (startTime >= holdTime)
             {
                     Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                     transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);
             }
         }
     }

I need to if touch hold 2 seconds character moved to the touch spot and if just one tap turned towards to spot

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aditya · Oct 20, 2016 at 08:54 AM

 bool wasLongTouch;
 
 public void Move(){
     if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began){
         Invoke("changeBool", 2f);
     }
 
     if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended){
         if(!wasLongTouch){
             // Just Rotate the player
             CancelInvoke("changeBool");
         }else{
             // Long Press Detected, move player to touch position
         }
     }
 
     void changeBool(){
         wasLongTouch = true;
     }
 }

Tip : disable touch when player is moving or rotating ... and when there was a long touch do not forget to change wasLongTouch bool back to false when player finished moving

Don't copy paste this in your code as this is not tested

Comment
Add comment · Show 5 · 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 Easy_ · Oct 20, 2016 at 09:33 AM 0
Share
     public void $$anonymous$$ove()
     {
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Invoke("changeBool", 2f);
         }
 
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)
         {
             if (!wasLongTouch)
             {
                 // Just Rotate the player
                 CancelInvoke("changeBool");
             }
             else
             {
                 // Long Press Detected, move player to touch position
             }
         }
     }
     public void changeBool()
     {
         wasLongTouch = true;
     }
 }

nope, i try to rewrite code using invoking didn't work...

avatar image aditya Easy_ · Oct 20, 2016 at 10:20 AM 0
Share

Dont use TouchPhase.Stationary, use TouchPhase.Began

avatar image Easy_ aditya · Oct 20, 2016 at 10:35 AM 0
Share

when phase == began it moves just a moment and stop to move i use this

 Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                 transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);

Show more comments
avatar image Easy_ · Nov 18, 2016 at 01:18 PM 0
Share

update: If you are stationary holding finger on screen player start moving to that point.

     public float duration = 10f;
     public float rotationSpeed = 5f;
     public float tapHolding;
 
     private bool flag = false; //Set to true on click. Reset to false on reaching destination
     private float zAxis; //vertical position of the gameobject
     private Vector3 endPoint; //destination point
     private float nextFire;
     // Use this for initialization
     void Start()
     {
         zAxis = gameObject.transform.position.z; //save the y axis value of gameobject
     }
     // Update is called once per frame
     void Update()
     {
         $$anonymous$$ove();
     }
     public void $$anonymous$$ove()
     {
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             tapHolding += Time.deltaTime;
             if (Physics.Raycast(ray, out hit) && tapHolding >= 2)
             {
                 flag = true;//set a flag to indicate to move the gameobject
                 endPoint = hit.point; //tap position
                 endPoint.z = zAxis; //as we do not want to change the z axis value based on touch position, reset it to original z axis value
                 Debug.Log(endPoint);
                 //check if the flag for movement is true and the current gameobject position is not same as the tapped position
             }
         }
         else
         {
             tapHolding = 0;
         }
         if (flag && !$$anonymous$$athf.Approximately(transform.position.magnitude, endPoint.magnitude))
         {
             //move the gameobject to the desired position
             float targetAngle = $$anonymous$$athf.Atan2(endPoint.y, endPoint.x) * $$anonymous$$athf.Rad2Deg;
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), rotationSpeed * Time.deltaTime);
             transform.position = Vector2.Lerp(transform.position, endPoint, 1 / (duration * (Vector2.Distance(transform.position, endPoint))));
             tapHolding = 0;
         }
         //set the movement indicator flag to false if the endPoint and current gameobject position are equal
         else if (flag && $$anonymous$$athf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
         {
             flag = false;
             Debug.Log("I am here");
         }
     }
 }
avatar image
0

Answer by Easy_ · Oct 20, 2016 at 10:15 AM

     void Update()
     {
         Move();
     }
     public void Move()
     {
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             float startTime = Time.time;
             if (startTime >= holdTime)
             {
                 Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                 transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);
             }
         }
     }
 }

i have this code, when enter in playmode character move to touch point after two seconts hold, but then i need to reset timer called "startTime" after touch is ended. Then do another touch hold to move after 2 second and so on.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Touch for iOS, action occurs on release -2 Answers

Multitouch Hold and Move 0 Answers

How to make a 2D mobile tap jump with small jump and if I Hold a touch it would jump higher 0 Answers

Holding GUI Button Touch to Rotate Object 0 Answers

Touch Hold to open up a submenu 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