Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 org743 · Aug 12, 2021 at 10:40 PM · touchtrajectory

Hello, everything works only that when the drag of the collider.

// Hello, everything works only that when the drag of the collider comes out, it stops following the finger, I want it to come out, follow it until you release the drag, please help,good a question my code applies the drag well, but when it leaves the radius of the collider, the object does not follow the trajectory of the drag it stays stopped until it touches the collider again, any help?.

using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SosteneryMantener : MonoBehaviour {

 // touch offset allows ball not to shake when it starts moving
 float deltaX, deltaY;


 // reference to Rigidbody2D component
 Rigidbody2D rb;


 // ball movement not allowed if you touches not the ball at the first time
 bool moveAllowed = false;


 // Use this for initialization
 void Start()
 {


     rb = GetComponent<Rigidbody2D>();


     // Add bouncy material tha ball
     PhysicsMaterial2D mat = new PhysicsMaterial2D();
     mat.bounciness = 0.99f;
     mat.friction = 0.4f;
     GetComponent<CircleCollider2D>().sharedMaterial = mat;


 }


 // Update is called once per frame
 void Update()
 {


     // Initiating touch event
     // if touch event takes place
     if (Input.touchCount > 0)
     {


         // get touch position
         Touch touch = Input.GetTouch(0);


         // obtain touch position
         Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);


         // get touch to take a deal with
         switch (touch.phase)
         {


             // if you touches the screen
             case TouchPhase.Began:


                 // if you touch the ball
                 if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
                 {


                     // get the offset between position you touches
                     // and the center of the game object
                     deltaX = touchPos.x - transform.position.x;
                     deltaY = touchPos.y - transform.position.y;


                     // if touch begins within the ball collider
                     // then it is allowed to move
                     moveAllowed = true;


                     // restrict some rigidbody properties so it moves
                     // more  smoothly and correctly
                     rb.freezeRotation = true;
                     rb.velocity = new Vector2(0, 0);
                     rb.gravityScale = 0;
                     GetComponent<CircleCollider2D>().sharedMaterial = null;
                 }
                 break;


             // you move your finger
             case TouchPhase.Moved:
                 // if you touches the ball and movement is allowed then move
                 if (GetComponent<CircleCollider2D>() == Physics2D.OverlapPoint(touchPos) && moveAllowed)
                     rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY));
                 break;


             // you release your finger
             case TouchPhase.Ended:


                 // restore initial parameters
                 // when touch is ended
                 moveAllowed = false;
                 rb.freezeRotation = false;
                 rb.gravityScale = 2;
                 PhysicsMaterial2D mat = new PhysicsMaterial2D();
                 mat.bounciness = 0.99f;
                 mat.friction = 0.4f;
                 GetComponent<CircleCollider2D>().sharedMaterial = mat;
                 break;
         }
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

192 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

Related Questions

How to detect a touch inside a GUI 0 Answers

Drag Object with Touching 0 Answers

touch of the screen interacts along with buttons on the screen 1 Answer

Is this possible?? (details below) [with pictures] 0 Answers

how to detect letters drawn on screen using touch 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