# Dragging object out of position

I am really having a hard time with one of my scripts. Mainly because my coding experience stops here.

Imagine you have a desk with a drawer inside. In this case the desk is invisible for now. I have a basic “drag rigidbody” script so I can drag objects around. The drawer has fixed positions so it can only move into one direction (only Z, or X). Gravity is off as well.

So now with the dragging script you can drag the drawer out of the desk but it doesn’t have a limit of where it can go. So I needed to write a script to stop this. This script simply teleports the drawer back into it’s original position. NOTE: I have tried placing invisible objects as colliders but you’re still able to drag them through.

With my script below you would maybe say: simple, don’t teleport the drawer back! I tried, it stops at the limit, but you can still drag it further out (even with the velocity at 0).

So what am I trying to achieve?

I want to make it possible to drag the drawer out of the desk, but with limits.
Take a look at my beautiful picture below. In this case we only drag the drawer on the Z axis. In this case that’s to left and right. At start of the game the drawer is neatly inside the desk. You may not move the drawer further inside the desk, like you can’t in real life. But you may also not drag the drawer too far out.

``````//if current object moves too much out of position, it will be teleported back into its original position.

var objectPosX : float;
var objectPosY : float;
var objectPosZ : float;
var originalPos : Vector3;

function Start() {
//store all current position and rotation for the object
objectPosX = transform.position.x;
objectPosY = transform.position.y;
objectPosZ = transform.position.z;
originalPos = Vector3(objectPosX,objectPosY,objectPosZ);
}

function Update() {

if(objectPosX < transform.position.x - renderer.bounds.size.x){
transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
rigidbody.velocity = Vector3(0,0,0);
}

if(objectPosZ < transform.position.z - renderer.bounds.size.z){
transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
rigidbody.velocity = Vector3(0,0,0);
}

if(objectPosX > transform.position.x + renderer.bounds.size.x){
transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
rigidbody.velocity = Vector3(0,0,0);
}

if(objectPosZ > transform.position.z + renderer.bounds.size.z){
transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
rigidbody.velocity = Vector3(0,0,0);
}

}
``````

This script will move an object between 2 positions, with the variable targetpos
you don’t really need a rigidbody for this tho, well I wont use one

``````public float zOffset, speed;
private Vector3 firstPos, maxPos, taretPos;

void Start()
{
//firstPos will be the start position
firstPos = transform.position;
//maxPos will be the maximum position (in z)
maxPos= firstPos + new Vector3(0, 0, zOffset);
//change the targetPos to a value you want
targetPos = startPos;

}

void Update()
{
//before we try to move the object, we check if the target position is legit:
// targetPos.z must be bigger than the startPos or smaller than the max position
if(targetPos.z > startPos.z && targetPos.z < maxPos.z)
{
//this will smoothly move the object to its target position
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * speed);
}

}
``````

So basicly, what ever input you take care off, just change the targetPos to your desired value. I haven’t tried this in unity or checked for compile errors, so let me know if there’s anything wrong

I imagine the easiest way to go about this is to clamp the min and max positions of the drawer on the Z and X axes using Mathf.Clamp. Set the original position and use Vector3.Lerp or Vector3.MoveTowards or something similar to snap it back to it’s original position when let go.