"Unity3d base" let the object move ②--ugui mouse click Frame

Source: Internet
Author: User

Background

The previous article through the mouse to move the code is very simple, so see not many people, but still want to thank "armed Sanzang" in the blog park to give comments and support, I hope he can see the second, in fact, can be very simple, but also the spirit of self-control, the key is the code is less is my favorite way, Also confirms the complexity of the Unity3d (the same functionality is encapsulated multiple times), the code is as follows:

public class Debugtest:monobehaviour{recttransform M_rect;void Start () {m_rect = Getcomponent<recttransform> ();} void Update () {if (Input.getmousebuttonup (0)) {m_rect.position = Input.mouseposition;}}}

  

The effect is as follows (as the effect is basically similar, a preview of the previous article):

This article mainly solves the last question, that is, moving objects by such a mouse is not smooth enough, no matter how far it is to teleport past, the visual experience is not good enough.

This article is intended to move by frame by update. Achieve smooth moving effect

Principle

1, record the mouse click Point, this point is if the screen coordinates
2. Convert this screen coordinate to world coordinates
3. Use mouse World coordinates-world coordinates of the Elves | | You can also use the mouse's local coordinates-the sprite's local coordinates
4, through 3 to get the direction of movement, do interpolation in the update inside the Move Wizard
5. Moving sprites can be moved using world coordinates, or they can be moved by local coordinates

Implementation 1

The Ugui event system through the Ipointerclickhandler implementation of the mouse click, and then move through the frame in update, all coordinates using localposition, the principle is very simple here the code is not verbose

   Private Recttransform Childplaytransform;    private float speed =10.0f;    Private Vector2 pointclickpostion;    Private Vector3 currentlocalpostion;    Private Vector3 Movedirect; Use the this for initialization void Start () {//Get image transform Childplaytransform = transform. Find ("Image").       Getcomponent<recttransform> (); }//update is called once per frame void Update () {currentlocalpostion = Childplaytransform.loc            Alposition;        Vector3 targetposition = movedirect * speed + currentlocalpostion;        Childplaytransform.localposition = Vector3.lerp (currentlocalpostion, targetposition, Time.deltaTime*4f);        } public void Onpointerclick (Pointereventdata eventData) {Vector2 localpoint; Detection within the rectangular range, global mouse click Point, to Local point conversion recttransformutility.screenpointtolocalpointinrectangle (transform. Getcomponent<recttransform> (), Eventdata.position, Eventdata.entereventcamera, out LocaLpoint);        Pointclickpostion = Localpoint;        Movedirect = new Vector3 (pointclickpostion.x, POINTCLICKPOSTION.Y, 0)-currentlocalpostion;        movedirect.z = 0;    Movedirect.normalize (); }

  

Implementation 2

Using the Unity3d input system input to enter the mouse position, and then update in the use of world coordinates for the sprite-by-frame translation, the code is as follows:

Private Transform Spritetransform;  Vector3 direction;  Vector3 spritecurentpoistion;  Vector3 targetposition;  float speed = 2.0f; void Start () {Spritetransform = transform.  Find ("Image") as Transform;      }//update is called once per frame void Update () {spritecurentpoistion = spritetransform.position;      vector addition (toward mouse direction) Targetposition = direction * speed + spritecurentpoistion;  Spritetransform.position = targetposition; } public void Onpointerclick (Pointereventdata eventData) {Vector3 mouseworldpointer = new Vector3 (eventdata.positi      on.x, EVENTDATA.POSITION.Y, 0);      The screen coordinates are converted to world coordinates//vector3 mouseworldpointer = Camera.main.ScreenToWorldPoint (Mousescreenpointer);      Vector subtraction gets the direction pointing to the mouse point direction = mouseworldpointer-spritetransform.position;      direction.z = 0; Direction.      Normalize (); Debug.Log (String. Format ("x:{0},y:{1}--{2},{3}", Mouseworldpointer.x, Mouseworldpointer.y, eventdata.position.x,  EVENTDATA.POSITION.Y)); }

  

Issue record

At the time of the test encountered some small problems, hereby record, also hope that the master passing generous answer

1, about the dominant interpolation function

Vector3.lerp (Currentlocalpostion, targetposition, time.deltatime*4f);

Actually this is a formula is also very simple, that is, I see the tutorial (Unity3d) When time.deltatime This value is very large, and actually found that the value is very small, resulting in the time of interpolation is very tiny movement, I do not know why can only be multiplied by a coefficient

2. The function hint about input of unity3d is very weak.

such as Eventdata.position and input.mouseposition exactly what coordinates are screen coordinates or world coordinates, the document ambiguity does not explain

Summarize

In the realization of such a small function, a profound effect can be used in many ways, encountered some knowledge points, the principle is very simple, but you do not go deep but get no answer. The world is like fast food, and it gets too fast.

"Unity3d base" let the object move ②--ugui mouse click Frame

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.