- Home /
Drag and release
I need help with (what I believe to be simple but I don't know) something (for android): I have a 2D ball, that is on a simple square table, I want to drag from that ball and release and upon release the ball goes in that direction or in opposite direction if you drag "behind" the ball. I don't need aim, or arrows or anything. Just put your finger on the ball then drag and release. I searched for 2 days on forums and youtube and everywhere but I couldn't find what I need. I found something but it's really outdated and does not help me. Thank you for your help! And I am sorry for taking your time if there is already something answered that I didn't find!
Answer by raulGLD · Oct 12, 2018 at 11:19 AM
EDIT: THE RIGHT CODE IS NOW POSTED I found it, hope this helps someone else too!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipe : MonoBehaviour {
Rigidbody2D rbody;
Vector2 startpos;
Vector2 endpos;
float power = 5f; // power of shot
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0))
{
endpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
LaunchBall();
}
}
}
void OnMouseDown()
{
startpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void LaunchBall()
{
Vector2 direction = (startpos - endpos); // swap subtraction to switch direction of launch
rbody.AddForce(direction * power, ForceMode2D.Impulse);
}
Still, I need help with something. I have an object that moves when I swipe on the screen but it also rotates and if I swipe again the same way the object might go another way due to the fact that it rotated. I don't want to freeze it's rotation I just want to make it work regardless it's rotation. Could you please help me? Thank you!
AddForce is immune to object rotations. So the rotation of an object does not affect the result of the added velocity. If you want to cancel all current momentum and just move in the direction you swipe, don't use AddForce but ins$$anonymous$$d set the velocity directly.
Note that your code has a general flaw and may only work for a single object. Currently you only record the start position when you have a mouse down on the object. However the mouse up event will always be detected regardless of where you clicked down initially. So if you release your mouse all objects with this script will react to the mouse up but with their old start position.
If you want to accelerate single objects you should use a boolean to remember that you clicked on this object and only apply a force on $$anonymous$$ouseUp when that boolean is true. Don't forget to reset that bool once you applied the force.
If all objects with this script should be affected you shouldn't use On$$anonymous$$ouseDown since that is only detected for the object the mouse is over. As far as i remember your original code actually used Get$$anonymous$$ouseButtonDown inside Update.
Yes you are right about that start position. I tried but can't figure out how to make it reset the ball position so I can swipe wherever I want not just on the ball start position. Can you please help me? Thank you!