Hello there,
my situation: I get the mouse position in 3D space while dragging the object, and it should move to this position. The script works very well so far, but it doesn't include collisions with my 4 walls. ( The scene is a small room ) I like the result I get with this script, so rigidbodys and physics do not come into question. Besides, I already tried that with the help of user Tylo. Old question here
Complete Drag script:
var planeNormal : Vector3 = Vector3( 0, 1, 0 );
var currentProjectedOnPlaneNormal : float;
var cameraProjectedOnPlaneNormal : float;
var wantedDistanceFromCamera : float;
var pOnPlaneNormal : float;
private var rememberedYPos: float;
function OnMouseDown ()
{
// An extra hack: If Up is (0,1,0), then we remember the y-coordinate, so we don't change it
rememberedYPos = transform.position.y;
}
function OnMouseDrag ()
{
Drag();
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
function Drag()
{
var p: Vector3;
var mainCamera = FindCamera();
// Position on the near clipping plane of the camera in world space
p = mainCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, mainCamera.nearClipPlane));
// Position relative to the eye-point of the camera
p -= mainCamera.transform.position;
currentProjectedOnPlaneNormal = Vector3.Dot( planeNormal, transform.position );
cameraProjectedOnPlaneNormal = Vector3.Dot( planeNormal, mainCamera.transform.position );
wantedDistanceFromCamera = cameraProjectedOnPlaneNormal - currentProjectedOnPlaneNormal;
pOnPlaneNormal = Vector3.Dot( planeNormal, p );
p *= wantedDistanceFromCamera / -pOnPlaneNormal;
p += mainCamera.transform.position;
transform.position = p;
// If we know that the object should only move in its x,z coordinates, we can
// make sure we don't drift slowly off the plane by doing this:
transform.position.y = rememberedYPos;
}
Is it possible to set a "drag area" so I can't drag out of the room? I thought of something like this:

Because the object will later be rotated, it would be great if the individual vertices could be read... so no vertice could get out of the room area. Is there a way to get this working? Thanks in advance for your time
Edit
I calculate a distance from each of the object corners to the room walls. Is the object been dragged out of the room, I substract the distance, to get it back to the desired drag area. This isn't working with rotation so far. For example, with a cube:
private var PointUpperLeft : Vector3;
private var calculatepoints : boolean = false;
private var NorthWall : float;
function Awake ()
{
NorthWall = GameObject.Find("Room").transform.localScale.z / 2;
//this gives me the max z position at the north side of my ground
}
function Update ()
{
if ( calculatepoints == true )
{
PointUpperLeft = Vector3( transform.position.x - (transform.localScale.x / 2), rememberedYPos, transform.position.z + (transform.localScale.z / 2) );
//this gives me the upper left corner of my object, calculated from the middle (square)
}
if ( ( PointUpperLeft.z > NorthWall )
{
var dist = PointUpperLeft.z - NorthWall;
print ("Distance exceeded to NorthWall " + dist);
//substract excess position
transform.position.z = transform.position.z - dist;
//new position of the Point
PointUpperLeft = Vector3( transform.position.x - (transform.localScale.x / 2), rememberedYPos, transform.position.z + (transform.localScale.z / 2) );
}
function OnMouseDrag ()
{
Drag();
calculatepoints = true;
}
function OnMouseUp ()
{
calculatepoints = false;
}
Thanks Tylo, I read your rigidbody section! I am using a distance variable to limit the area if I drag a part of the object out of the room. I've updated my question with an example code. You deserve your check for pointing me the right direction
– Baroni