usingUnityengine;usingSystem.Collections; Public classModelsposchange:monobehaviour {raycasthit hit; PrivateTransform Icon; BOOLIsmove =false; voidfixedupdate () {if(ismove) {ray Ray=Camera.main.ScreenPointToRay (input.mouseposition); if(Physics.raycast (Ray, outHit -)) && (NULL!=Hit.collider)) {Debug.drawline (Ray.origin, hit.point); Icon= transform. Getcomponent<transform>(); Vector3 Screenpos=Camera.main.WorldToScreenPoint (icon.position); Vector3 Offset= Icon.transform.position-camera.main.screentoworldpoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, screenpos.z)); Vector3 Mousepos=NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, screenpos.z); Vector3 Worldpos=Camera.main.ScreenToWorldPoint (Mousepos); Icon.position=Worldpos; } Transform.renderer.material.color=Color.green; if(Input.getkey (Keycode.delete)) {Destroy ( This. Gameobject); } } Else{Transform.renderer.material.color=Color.White; } } voidOnMouseDown () {Ismove= !Ismove; }}
View Code
Share: Unity3d model follows the mouse movement function of the Little foot (screen coordinates and three-dimensional space coordinate transformation)