- Home /
Trying to drag an object with touch using Rigidbody2D.MovePosition
Hello
I have this spirit object which i want to move by dragging (dragging the object to move it on screen) using Rigidbody2D.MovePosition.
I am quite newb in unity . so maybe i missed something i do not know.
Whatever i tried doesn't make the object move by touch. The object is Rigidbody2D with Box 2d collider. Mass is set to 10. Liner drag is 0. gravity Scale is 0.01. I tried with is Kinamtic on or off.
What i have tried:
private var speed : Vector2 = Vector2 (3, 0);
function FixedUpdate () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
rigidbody2D.MovePosition(rigidbody2D.position + speed * Time.deltaTime);}
}
Also, does MovePosition is based on Rigidbody2d Physics? I can't manage to make the object move with touch.. I am using Unity version 4.5.
Any help would be appreciated!
Is your rigidbody2D not moving at all or is it not moving as per the position of the touch input?
Could you provide details as to how you expect your script to work and what you are getting?
Answer by RakshithAnand · Jul 01, 2014 at 09:58 AM
Try this instead of using rigidbody2D.MovePosition:
float speed = 2f;
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchpos = Camera.main.ScreenToWorldPoint(touch.position);
yourSpriteObject.transform.position = Vector3.MoveTowards(yourSpriteObject.transform.position, touchpos, Time.deltaTime * speed);
}