- Home /
Moving GameObject with Add force and mouse drag
The picture above is what I am trying to accomplish. I want the player to be able to drag their finger and move the object around. Once the finger is removed I would like the object to keep moving. How would I do something like this? I don't know what is involved so I don't really know where to start. Even just a list of things to do with out code would be helpful because I don't know where to start.
Answer by Funlamb · Mar 16, 2015 at 02:22 AM
So I feel like I noted it pretty well. It was just as easy as just setting the gameObjest.rigidBody.velocity to the force that is found through the two points of the mouse.
public Vector3 gameObjectSreenPoint;
public Vector3 mousePreviousLocation;
public Vector3 mouseCurLocation;
void OnMouseDown()
{
//This grabs the position of the object in the world and turns it into the position on the screen
gameObjectSreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
//Sets the mouse pointers vector3
mousePreviousLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
}
public Vector3 force;
public Vector3 objectCurrentPosition;
public Vector3 objectTargetPosition;
public float topSpeed = 10;
void OnMouseDrag()
{
mouseCurLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
force = mouseCurLocation - mousePreviousLocation;//Changes the force to be applied
mousePreviousLocation = mouseCurLocation;
}
public void OnMouseUp()
{
//Makes sure there isn't a ludicrous speed
if (rigidbody.velocity.magnitude > topSpeed)
force = rigidbody.velocity.normalized * topSpeed;
}
public void FixedUpdate()
{
rigidbody.velocity = force;
}
Thank you for the script! A couple of things, though:
1) When throwing an object upwards, there are no physics that cause it to lose rigidbody.velocity.y, so it ascends to infinity. This is easily solved by including this:
void Update () {
if (force.y > 0.0f) {
force.y -= 0.1f;
}
2) Recently rigidbody.velocity has been phased out and must be replaced with GetComponent<Rigidbody>().velocity
.
3) I don't know why, but this script causes gameobjects to descend incredibly slowly.
Have an updated script with anti-ascension protection:
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//This prevents your thrown object from ascending to infinity and beyond. Disable if you're trying to throw Buzz Lightyear.
if (force.y > 0.0f) {
force.y -= 0.1f;
}
}
public Vector3 gameObjectSreenPoint;
public Vector3 mousePreviousLocation;
public Vector3 mouseCurLocation;
void On$$anonymous$$ouseDown() {
//This grabs the position of the object in the world and turns it into the position on the screen
gameObjectSreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
//Sets the mouse pointers vector3
mousePreviousLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
}
public Vector3 force;
public Vector3 objectCurrentPosition;
public Vector3 objectTargetPosition;
public float topSpeed = 10;
void On$$anonymous$$ouseDrag() {
mouseCurLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
force = mouseCurLocation - mousePreviousLocation;//Changes the force to be applied
mousePreviousLocation = mouseCurLocation;
}
public void On$$anonymous$$ouseUp() {
//$$anonymous$$akes sure there isn't a ludicrous speed
if (GetComponent<Rigidbody>().velocity.magnitude > topSpeed)
force = GetComponent<Rigidbody>().velocity.normalized * topSpeed;
}
public void FixedUpdate() {
GetComponent<Rigidbody>().velocity = force;
}
The object falls super slow, I made this version that let's go of the object after release
private Rigidbody2D rigidbody; public Vector3 gameObjectSreenPoint; public Vector3 mousePreviousLocation; public Vector3 mouseCurLocation; private Vector3 offset; private bool isDragging = false;
public Vector3 force;
public float topSpeed = 50;
private void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
void On$$anonymous$$ouseDown()
{
//This grabs the position of the object in the world and turns it into the position on the screen
gameObjectSreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
//Sets the mouse pointers vector3
mousePreviousLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z));
}
void On$$anonymous$$ouseDrag()
{
mouseCurLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
force = mouseCurLocation - mousePreviousLocation;//Changes the force to be applied
mousePreviousLocation = mouseCurLocation;
Vector3 curPosition = Camera.main.ScreenToWorldPoint(mouseCurLocation) - offset;
transform.position = curPosition;
isDragging = true;
}
public void On$$anonymous$$ouseUp()
{
//$$anonymous$$akes sure there isn't a ludicrous speed
if (rigidbody.velocity.magnitude > topSpeed)
force = rigidbody.velocity.normalized * topSpeed;
isDragging = false;
}
public void FixedUpdate()
{
if (isDragging)
{
rigidbody.velocity = force;
}
}
Answer by Denvery · Mar 15, 2015 at 09:32 PM
I think you need to do 2 main things: 1. Checking the drag and moving objects with the finger 2. Check if finger is up and adding the Force to the GameObject.
May be your simplest solution's Update() can look like this:
bool _touchBegan = false;
private void Update()
{
if (Input.touchCount <= 0)
{
return;
}
Touch currentTouch = Input.GetTouch(0);
Vector2 currentTouchPosition = currentTouch.position;
Vector3 touchPointInWorld = Camera.main.ScreenToWorldPoint(new Vector3(currentTouchPosition.x, currentTouchPosition.y));
if (currentTouch.phase == TouchPhase.Began && _spriteRenderer.bounds.Contains(touchPointInWorld) && !_touchBegan)
{
_touchBegan = true;
return;
}
if (currentTouch.phase == TouchPhase.Moved && _touchBegan)
{
transform.localPosition = Camera.main.ScreenToWorldPoint(new Vector3(currentTouchPosition.x, currentTouchPosition.y));
return;
}
if (currentTouch.phase == TouchPhase.Ended || currentTouch.phase == TouchPhase.Canceled && _touchBegan)
{
//AddForce
_touchBegan = false;
return;
}
}
So you should to attach this script to the object which you want to drag.
_spriteRenderer.bounds.Contains(touchPointInWorld)
- checking if the touch's point in inside the bounds of you gameObject (in my case it is gameObject with SpriteRenderer, i.e. Unity 2d sprite).
//AddForce - this means that in this moment finger is Up and you may to add force by rigidbody.AddForce(...) or in the other way.
So it is the simplest solution, I hope this lines are readable and I hope they will help you:)
This is a great start. I need to change this to get used by the mouse for now. Should I mark this as the correct answer now or get it to work first then mark it as correct?
Thanks you got me in the right direction. I'll be referring to this when I need to get this on a phone and need to base the inputs on touch.
I'm glad that you've find nice solution for mouse. (I understood that you need this on a phone because you've wrote about fingers:)
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2d Platformer Horizontal Max Speed 2 Answers
Deteriorating force on object? 0 Answers
Roll a ball camera problem? 1 Answer