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
- <span style="font-size:10px;" >using System.Collections;
- Using System.Collections.Generic;
- Using Unityengine;
- Public class Camera:monobehaviour
- {
- Public Camera CA;
- Private Ray RA;
- private raycasthit hit;
- // Use this for initialization
- void Start ()
- {
- }
- //Update is called once per frame
- void Update ()
- {
- RA = ca. Screenpointtoray (input.mouseposition);
- if (Physics.raycast (RA, out hit ))
- {
- }
- }
- }</span>
Apply one: When the mouse is pressed, move the selected object with the mouse move
[CSharp]View PlainCopy
- <span style="font-size:10px;" >using System.Collections;
- Using System.Collections.Generic;
- Using Unityengine;
- Public class Camera:monobehaviour
- {
- Public Camera CA;
- Private Ray RA;
- private raycasthit hit;
- // Use this for initialization
- void Start ()
- {
- }
- //Update is called once per frame
- void Update ()
- {
- if (Input.getmousebutton (0))
- {
- RA = ca. Screenpointtoray (input.mouseposition);
- if (Physics.raycast (RA, out hit ))
- {
- Hit.collider.gameObject.transform.position = ca. Screentoworldpoint (new Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, HIT.COLLIDER.GAMEOBJECT.TRANSFORM.POSITION.Z));
- }
- }
- }
- }</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
- <span style="FONT-SIZE:12PX;" >using System.Collections;
- Using System.Collections.Generic;
- Using Unityengine;
- Public class Camera:monobehaviour {
- Public Camera CA;
- Private Ray RA;
- private raycasthit hit;
- private int flag = 0;
- // Use this for initialization
- void Start () {
- }
- //Update is called once per frame
- void Update () {
- if (input.getmousebuttondown (0))
- {
- RA = ca. Screenpointtoray (input.mouseposition);
- if (Physics.raycast (RA, out hit ))
- {
- if (flag = = 0)
- {
- flag = 1;
- } Else
- {
- Flag = 0;
- }
- }
- }
- if (flag = = 1)
- {
- Hit.collider.gameObject.transform.position = ca. Screentoworldpoint (new Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, HIT.COLLIDER.GAMEOBJECT.TRANSFORM.POSITION.Z));
- }
- }
- }
- </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