Unity3d Simple way for C # objects to move with objects

Source: Internet
Author: User

Private Vector3 lastpos = Vector3.zero;
private bool Istouch;
Use this for initialization
void Start () {

}

Update is called once per frame
void Update () {
Determine if the mouse is in the clicked State
if (Input.getmousebuttondown (0)) {
Istouch = true;
}
if (input.getmousebuttonup (0)) {
Istouch = false;
Lastpos Zero when you select the mouse
Lastpos = Vector3.zero;
}
In the click State, the object moves with the mouse
if (Istouch) {
referential screen coordinates.
Vector3 Screenpos = Camera.main.WorldToScreenPoint (transform.position);
if (lastpos! = Vector3.zero) {
Drag-and-drop processing
Mouse offset coordinates in one frame
Vector3 OffsetPos = Camera.main.ScreenToWorldPoint (New Vector3 (input.mouseposition.x,
INPUT.MOUSEPOSITION.Y,
SCREENPOS.Z))-Lastpos;
Transform.position + = OffsetPos;
}
Record Previous frame coordinates
Lastpos = Camera.main.ScreenToWorldPoint (New Vector3 (INPUT.MOUSEPOSITION.X,INPUT.MOUSEPOSITION.Y,
SCREENPOS.Z)); Convert to world coordinates

}
}
}

Unity3d Simple way for C # objects to move with 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.