Unity3d mouse click on the screen to control the movement of characters

Source: Internet
Author: User
Tags control characters

Today, we come together to implement a very common function in the RPG game, the mouse clicks on the screen to control the movement of characters. First of all, when we click on the screen, we follow a certain method, the screen of two-dimensional coordinates into three-dimensional coordinates, and then we launch from the camera position through the point of the ray, then this ray and the Earth plane intersection, is our target position. So after we've identified this position, all we need to do is move the character from the original position to the position and add the role animation. For the problem of role movement, if the complex point of the painting we may want to compute the direction vector of the movement, and for the starting point and end of the interpolation calculation to achieve, here we choose a relatively simple approach, in Unity3d, there is a LookAt () method, which can make the object rotation, Causes its z-axis to always point to the target object. Once we have finished the rotation, we need to move the object along the z-axis (forward). This enables the mouse to click on the control characters to move around this function. OK, now let's look at today's content together.

First create a scene, we prepare a terrain terrain and parallel light direction Light, and drag into our prepared figure model as shown:


Next, add an animation component to the model and set the animation's type to Legacy mode, as shown in the figure:



We set the default animation to idle, and next, we'll write script code:

Using Unityengine;

Using System.Collections;
  
  public class Peoplecontrol:monobehaviour {//target point coordinates private VECTOR3 mtargetpos; void Start () {} void Update () {//press the right mouse button if (Input.getmousebutton (1)) {//Get screen coordinates V
     Ector3 mscreenpos=input.mouseposition;
     Define Ray Ray Mray=camera.main.screenpointtoray (MSCREENPOS);
     Raycasthit Mhit;
         Determine if Ray hits ground if (Physics.raycast (Mray,out mhit)) {if (mhit.collider.gameobject.tag== "Terrain") {
         Obtaining the target coordinate mtargetpos=mhit.point; Let the main character face toward the target coordinates and move transform to the target.
         LookAt (Mtargetpos); Play run animation transform.gameobject.getcomponent<animation> ().
       Play ("Run"); Transform.
      Translate (Vector3.forward * 0.5F); if (Input.getmousebuttonup (1)) {transform.gameobject.getcomponent<animation& When the right mouse button is released}}// gt; ().
    Play ("Idle"); }
  }
}
After binding the script to the role, we can see the following effect, haha:


This is today's content, of course, we still need to solve the problem of how to avoid collisions with other objects when people move around, as well as the subsequent optimization problems, which we will be in the later article for everyone to answer, thank you. Like me, please remember my name.

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.