Drag objects in the scene with mouse in unity and zoom with the mouse wheel

Source: Internet
Author: User

Add a plan,camera,directional light,cube to the scene. Add two scripts scrollerscirpt (hangs on camera), Cubedragscript (hangs on cube).

1. Mouse wheel zoom: The camera lens closer or pull away, adjust the camera angle of view can be achieved, the main implementation code is as follows:

void Update () {        ///mouse wheel effect        //camera.main.fieldofview Camera's field of view        //camera.main.orthographicsize orthographic projection of the camera        //zoom out        if (Input.getaxis ("Mouse scrollwheel") < 0)        {            if (Camera.main.fieldOfView <= 100)                 Camera.main.fieldOfView + = 2;            if (Camera.main.orthographicSize <=)                Camera.main.orthographicSize + = 0.5F;        }        Zoom in        if (Input.getaxis ("Mouse scrollwheel") > 0)        {            if (Camera.main.fieldOfView > 2)                Camera.main.fieldOfView-= 2;            if (Camera.main.orthographicSize >= 1)                Camera.main.orthographicSize-= 0.5F;        }}

2. Mouse implementation Drag the object in the scene:

The solution is to convert the world coordinates into screen coordinates, and then calculate the amount of movement between the object and the mouse, the loop mouse is pressed to get the current position of the mouse, plus the calculated movement, the new coordinates assigned to the physical on the line. The main is to open a synergistic program (Corountine) to handle

The main code is as follows:

Use the This for Initializationvoid Start () {Startcoroutine (OnMouseDown ());} IEnumerator OnMouseDown () {//Converts an object from a world coordinate system to a screen coordinate system Vector3 Screenspace = Camera.main.WorldToScreenPoint (tran sform.position)////coordinate to screen coordinates//complete two steps 1. Since the coordinate system of the mouse is 2 dimensions, it needs to be converted to a 3-dimensional world coordinate system////2. The mouse can only be computed with 3-dimensional coordinates Position and physical distance, offset is the distance//the mouse screen coordinates into three-dimensional coordinates, and then calculate the distance between the object position and the mouse Vector3 offset = Transform.position-camera.main.screen        Toworldpoint (New Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, screenspace.z)); while (Input.getmousebutton (0)) {//Gets the 2-dimensional coordinate system position of the mouse now Vector3 curscreenspace = new Vector3 (Input            . mouseposition.x, INPUT.MOUSEPOSITION.Y, screenspace.z);            Converts the current mouse's 2-D position to a 3-D position, plus the mouse movement Vector3 curposition = Camera.main.ScreenToWorldPoint (curscreenspace) + offset;            Curposition is the position attribute Transform.position = Curposition that the object should move vector to transform; Yield return new WAITFORFIXEDUPDAte (); This is important, loop execution}}

The scene is as follows

Drag objects in the scene with mouse in unity and zoom with the mouse wheel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.