2d Gesture. Drag and Release. Keep velocity

Hi Guys,

I’ve written a simple flick app using code suggestions found on the forums - but it really does not feel fluid.

What I’m really trying to do is have a very fluid drag/release ability that after you release the dragged item continues to follow it’s velocity (with some drag applied to it to slow it down overtime).

I’m also hoping to do this in a 2d game. Basically picture a big board that you can drag ‘puck’ shaped items around and when you release - they continue to keep velocity based on the released velocity.

Anyhow - When I get home I’ll post some code that I have - at the moment I attempt to apply a force to the gameobjects - but something is broken and they just shoot out of view.

Any help is Greatly appreciated.

Here is what I’m currently using. It works somewhat well and adjusting the mass (as suggested) does help. anyone have any good advice to make this work fluid - it’s somewhat chunky at the moment and the ball may roll randomly at times.

using UnityEngine;
using System.Collections;

public class Flick : MonoBehaviour {
	
	private Vector3 screenPoint;
	private Vector3 offset;
	private Vector3 curScreenPoint ;
	private Vector3 curPosition ;
	public Vector3 startingPosition;
	
	public bool lockXPos = false;
	public bool lockYPos = false;
	public bool lockZPos = false;
	
//	public float velocity = 0;
	
	public Vector3 worldAngle;
	
	public Vector3 lastPos;
	
	public Vector3 velocity;
	
	void Update(){ 
		float force =100f;
		Debug.DrawRay(new Vector3((velocity.x * force), (velocity.y * force),(velocity.z * force)), Vector3.forward, Color.green, 0.5f);
	//	gameObject.rigidbody.AddForce(Vector3.forward);
		
	}

	
	void OnMouseDown () {
	   	rigidbody.AddForce(Vector3.zero);
	    screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
		startingPosition = gameObject.transform.position;
	    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
	    Screen.showCursor = false;	
		
	}
	 
	void OnMouseDrag() { 
	    curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
	    curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
		
		if(this.lockYPos){
			curPosition.y = startingPosition.y;
		}
		
		lastPos = gameObject.transform.position;
		//Debug.Log(lastPos);
	    transform.position = curPosition;
		//gameObject.rigidbody.MovePosition(curPosition);
	}
	 
	void OnMouseUp(){
	
		Screen.showCursor = true;
		
	
            Vector3 offset = lastPos - transform.position;
            float sqrLen = offset.sqrMagnitude;
            Debug.Log(sqrLen);
            
        
		

		velocity = gameObject.transform.position - lastPos;
		float distance = Vector3.Distance(gameObject.transform.position, lastPos);
		Debug.Log(distance);
	
		
		//gameObject.rigidbody.AddForce(new Vector3((worldAngle.x * force), (worldAngle.y * force), (worldAngle.z * force)));
		gameObject.rigidbody.AddForce(velocity * distance * 2000);
		//gameObject.rigidbody.AddForce(new Vector3((velocity.x * force), (velocity.y * force),(velocity.z * force)));

    }
	
	
	void GetSpeed() {


	
	}

	
	void GetAngle () {

//	    worldAngle = Camera.main.ScreenToWorldPoint(new Vector3 (lastTouch.x, lastTouch.y,  lastTouch.z));
		//Debug.Log (worldAngle);

	}
	
	
}

The first thing that comes to mind is that the mass of your rigidbody is too low.

The second thing that comes to my mind is the input method. I’m assuming we have a Vector2 representing the users last stroke direction, and a magnitude for the time they had pressed. This would give us an x and y velocity to just use rigidbody2D.AddForce( v2 * -mag ). Perhaps negative magnitude if it was the pull back method you mentioned. I would inspect this value and assure the x, y, and magnitude components are what you expect.

Thanks Feyyd.
As soon as I get back home I’ll play with my mass value and take a look at those values.

I’ll also post my current script.

(currently on vacation - so will have to wait 6 days:()