Hello everybody;
i want to an object drag but the object does not detect collision or collider in 3D space.
i have a code for dragging on the gameObjects;
// variable definitions
var planeNormal : Vector3 = Vector3( 0, 1, 0 );
var currentProjectedOnPlaneNormal : float;
var cameraProjectedOnPlaneNormal : float;
var wantedDistanceFromCamera : float;
var pOnPlaneNormal : float;
private var rememberedYPos: float;
var controlDrag:boolean;
/* built-in functions */
// Use this for initialization
function Start()
{
controlDrag=true;
}
function Update () {
}
function OnMouseDown ()
{
controlDrag=true;
// 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 OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag=="Duvar"){
controlDrag=false;
}
}
function OnCollisionExit(collision:Collision){
if(collision.gameObject.tag=="Floor"){
}
}
function OnCollisionStay(collision:Collision){
if(collision.gameObject.tag=="Wall"){
controlDrag=false;
}
}
function OnMouseDrag ()
{
if(controlDrag){
Drag();
}
}
/* user defined functions */
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;
}
i want to move game object with the same time mouseCursor. And also the gameObjects should detect the colliders and they do not go out the Walls.
In shortly, Not only the gameobject move the floor like "Transform movement " but also detect any physics around its(Wall or another game object).![]()
Any help i will be very appriciate…