Home > other >  Unity, drag and drop objects, not out of the room, the room metope is uneven, drag and drop is alrea
Unity, drag and drop objects, not out of the room, the room metope is uneven, drag and drop is alrea

Time:09-25


Drag the object, the wall can't continue to drag wall,
Drag and drop is already written,
Using System. The Collections;
Using UnityEngine;
Public class Tuozhuai: MonoBehaviour
{
//objects screen space coordinates,
Vector3 targetScreenLocation;
//the world of the target object space coordinates
Vector3 targetWorldLocation;
//target object space transform component
The Transform transform1;
//mouse screen space coordinates
Vector3 mouseLocation;
//offset
Vector3 offset.
//camera movement,
Private Camera camera1;
//
Public static Tuozhuai _instance.
Void the Start ()
{
Camera1=GameObject. Find (" fushi "). The GetComponent (a);
}
Void the Update ()
{

}
Void Awake ()
{
Transform1=transform;
_instance=this;
}
Void OnCollisionEnter (Collision collisionInfo)
{
The Debug Log (" collision to the object's name is: "+ collisionInfo. GameObject. Name);
}
IEnumerator onm ouseDown ()
{
Print (the transform. The position);
//the world space coordinates of target object into his own screen space coordinates
TargetScreenLocation=camera1. WorldToScreenPoint (transform. The position);
Print (targetScreenLocation);
//store the mouse screen space coordinates (Z value using the target screen space coordinates)
MouseLocation=new Vector3 (Input) mousePosition) x, Input the mousePosition. Y, targetScreenLocation. Z);
//calculate the target object with the mouse in the world in space and the offset
Offset=the transform. The position - camera1. ScreenToWorldPoint (mouseLocation);
//the left mouse button press
While (Input. GetMouseButton (0))
{
//store the mouse screen space coordinates (Z value using the target object screen space coordinates)
MouseLocation=new Vector3 (Input) mousePosition) x, Input the mousePosition. Y, targetScreenLocation. Z);
//screen coordinates the mouse into the world coordinate
TargetWorldLocation=camera1. ScreenToWorldPoint (mouseLocation) + offset;
Transform1. Position=targetWorldLocation;
//update the target object world space coordinates of the
//wait for fixed update
Yield return new WaitForFixedUpdate ();
}
}
}

CodePudding user response:

I didn't think a good way to deal with this problem, also can't find a good way,

My approach is that when a collision, drag down the location of the object, as long as in contact condition back to the location of the collision,

Void OnCollisionEnter (Collision Collision) {
Touch_position=the transform. The position;
}

Void OnCollisionStay (Collision collisionInfo) {
The transform. The position=touch_position;
}

Do so reluctantly can realize the request of the building Lord, however, 1, and the surrounding objects must be fixed, or will be hit, 2, and drag, quickly will occur through the bug,

CodePudding user response:

reference 1st floor wuyt2008 response:
I didn't think a good way to deal with this problem, also can't find a good way,

My approach is that when a collision, drag down the location of the object, as long as in contact condition back to the location of the collision,

Void OnCollisionEnter (Collision Collision) {
Touch_position=the transform. The position;
}

Void OnCollisionStay (Collision collisionInfo) {
The transform. The position=touch_position;
}

Do so reluctantly can realize the request of the building Lord, however, 1, and the surrounding objects must be fixed, or will be hit, 2, and drag, quickly will occur through the bug,


Afraid of wall when a moving object with rays of attachment to determine whether to wear if it is to draw the first collision point

CodePudding user response:

Learning, should be a positive solution!