[Unity] Drag objects to move - Qiita
As mentioned above, I want to move the object with the mouse rack, but if I just refer to it, the center of the object will come to the mouse position.What should I do if the center of the object was moved by the mouse instead of the mouse?
c# unity3d
The hit
obtained from Physics.Raycast()
contains the world coordinates hit.point
of the point of contact with the ray, so take advantage of this.
Find the value to move the depth of the contact point.
private float depth;
depth = Camera.main.transform.InverseTransformPoint(hit.point).z;
Find the relative position vector from the target object so that the contact point can be picked up.
private Vector 3 offset;
offset = hit.point-this.transform.point;
Reflect these values when you move them.
Vector3 mousePos=Input.mouthPosition;
mousePos.z = depth;
moveTo=Camera.main.ScreenToWorldPoint(mouthPos);
transform.position=moveTo-offset;
© 2024 OneMinuteCode. All rights reserved.