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;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613041601im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with touch button (can't call a function) 0 Answers
Some things wrong with swipe move! 0 Answers
Drag two Object by Touch At the same time? 0 Answers
Separate Input from Touch on GUI 0 Answers
make player jump when tap on screen 0 Answers