"Turn" Unity3d tips for Getting Started--mouse to pick and move objects

Source: Internet
Author: User

http://blog.csdn.net/sysujackjiao/article/details/69396274

First, the mouse to pick up the principle of the object

In Unity3d, the way to pick up objects (with collision properties) in the observation surface (Aspect) is generally as follows:

1. Declare an observed camera, a ray ray from the origin of the camera, and a raycasthit for detecting collisions;

2, Ray Ray is defined as starting from the camera origin and point to the current mouse coordinates (screen coordinates);

3. Define the collision point of the collision Raycasthit as Ray Ray and the object with Collision property.

The specific code is implemented as follows (C # code):

[CSharp]View PlainCopy
  1. <span style="font-size:10px;"  >using System.Collections;
  2. Using System.Collections.Generic;
  3. Using Unityengine;
  4. Public class Camera:monobehaviour
  5. {
  6. Public Camera CA;
  7. Private Ray RA;
  8. private raycasthit hit;
  9. // Use this for initialization
  10. void Start ()
  11. {
  12. }
  13. //Update is called once per frame
  14. void Update ()
  15. {
  16. RA = ca. Screenpointtoray (input.mouseposition);
  17. if (Physics.raycast (RA, out hit ))
  18. {
  19. }
  20. }
  21. }</span>

Apply one: When the mouse is pressed, move the selected object with the mouse move

[CSharp]View PlainCopy
  1. <span style="font-size:10px;"  >using System.Collections;
  2. Using System.Collections.Generic;
  3. Using Unityengine;
  4. Public class Camera:monobehaviour
  5. {
  6. Public Camera CA;
  7. Private Ray RA;
  8. private raycasthit hit;
  9. // Use this for initialization
  10. void Start ()
  11. {
  12. }
  13. //Update is called once per frame
  14. void Update ()
  15. {
  16. if (Input.getmousebutton (0))
  17. {
  18. RA = ca. Screenpointtoray (input.mouseposition);
  19. if (Physics.raycast (RA, out hit ))
  20. {
  21. Hit.collider.gameObject.transform.position = ca. Screentoworldpoint (new Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,  HIT.COLLIDER.GAMEOBJECT.TRANSFORM.POSITION.Z));
  22. }
  23. }
  24. }
  25. }</span>

Application two: When the mouse clicks on an object, the object moves with the mouse, and when the mouse clicks again, the object is dropped.

[CSharp]View PlainCopy
  1. <span style="FONT-SIZE:12PX;"  >using System.Collections;
  2. Using System.Collections.Generic;
  3. Using Unityengine;
  4. Public class Camera:monobehaviour {
  5. Public Camera CA;
  6. Private Ray RA;
  7. private raycasthit hit;
  8. private int flag = 0;
  9. // Use this for initialization
  10. void Start () {
  11. }
  12. //Update is called once per frame
  13. void Update () {
  14. if (input.getmousebuttondown (0))
  15. {
  16. RA = ca. Screenpointtoray (input.mouseposition);
  17. if (Physics.raycast (RA, out hit ))
  18. {
  19. if (flag = = 0)
  20. {
  21. flag = 1;
  22. } Else
  23. {
  24. Flag = 0;
  25. }
  26. }
  27. }
  28. if (flag = = 1)
  29. {
  30. Hit.collider.gameObject.transform.position = ca. Screentoworldpoint (new Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,  HIT.COLLIDER.GAMEOBJECT.TRANSFORM.POSITION.Z));
  31. }
  32. }
  33. }
  34. </span>

Unity3d Mouse coordinates to world coordinates and local coordinates
Mouse coordinates to world coordinates and local coordinates:    Vector3 mouseposition2local () {        mouseposition = input.mouseposition;//Get screen coordinates        Mouseworldpos = Camera.main.ScreenToWorldPoint (mouseposition); screen coordinates to world coordinates        mouselocalpos = Transform.parent.transform.InverseTransformPoint (Mouseworldpos);//world coordinates to local coordinates        Return Correctpos (Mouselocalpos);    }

"Turn" Unity3d tips for Getting Started--mouse to pick and move objects

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.