[Unity3D self-study record] Character control in the free perspective state of Unity3D game development (1)

Before starting today's content, let us first learn some of the more important knowledge in Unity3D. Understanding this knowledge is the basis for us to start learning today's content.

     1. Input.GetAxis(): This method is used to return the value in the virtual coordinate system according to the coordinate axis name in Unity3D. Usually, the value range is between -1 and 1 when using the controller and keyboard to input. How do you understand this passage? Let's look at the following script:

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.   
  6.     //horizontal speed  
  7.     public float HorizontalSpeed = 2.0F;  
  8.     //vertical speed  
  9.     public float VerticalSpeed = 2.0F;  
  10.   
  11.     void Update()   
  12.     {  
  13.         //horizontal direction  
  14.         float h = HorizontalSpeed * Input.GetAxis("Mouse X");  
  15.         //Vertical direction  
  16.         float v = VerticalSpeed * Input.GetAxis("Mouse Y");  
  17.         //rotate  
  18.         transform.Rotate(v, h, 0);  
  19.     }  
  20. }  
This script is to rotate the object according to the position of the mouse to realize the observation of the object. From this script, we can see that by obtaining the input axis, we can obtain the direction of the mouse movement and realize the rotation control of the object. In Unity3D, we can view the axis names in the project through Edit->Project Setting->Input:


In the future, we will also use this method, and you can have a better understanding of this method.


     2. Euler angles eulerAngles: This value is a value of Vector3 type, and x, y, and z represent x degrees of rotation around the x-axis, y degrees of rotation around the y-axis, and z degrees of rotation around the z-axis, respectively. Therefore, the most intuitive form of this value allows us to modify the angle of an object directly in the form of a three-dimensional vector, such as the following script:

[html] view plaincopy
  1. float mY = 5.0;  
  2.   
  3. void Update ()   
  4. {  
  5.     mY += Input.GetAxis("Horizontal");  
  6.     transform.eulerAngles =new Vector3(0,mY, 0);  
  7. }  
     If you have understood the above, it is no surprise that this script will rotate around the Y axis in the direction the mouse moves in the horizontal direction as you wish. Normally, we don't set one of the Euler angle axes separately, e.g. eulerAngles.x = 10, because this will cause offset and undesired rotation. When setting them to a new value, set them all at the same time. Fortunately, we can convert a Vector3 type value into a quaternion through the Quaternion.Euler() method, and then achieve the same purpose by modifying Transform.Rotation.

    

     3. Interpolation: The so-called interpolation refers to interpolating a continuous function on the basis of discrete data , so that this continuous curve passes through all given discrete data points. Interpolation is an important method of discrete function approximation, which can be used to estimate the approximate value of the function at other points through the value of the function at a limited number of points. In some cases, if we want the process to be smoother, we can use the interpolation method to simulate the intermediate process. In Unity3D we can use two interpolation methods, namely linear interpolation Lerp and spherical interpolation SLerp. Let's look at the following script:

[csharp] view plaincopy
  1. void Rotating (float horizontal, float vertical)  
  2.     {  
  3.         // Create a new vector of the horizontal and vertical inputs.  
  4.         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);  
  5.           
  6.         // Create a rotation based on this new vector assuming that up is the global y axis.  
  7.         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);  
  8.           
  9.         // Create a rotation that is an increment closer to the target rotation from the player's rotation.  
  10.         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);  
  11.           
  12.         // Change the players rotation to this new rotation.  
  13.         rigidbody.MoveRotation(newRotation);  
  14.     }  
The method of interpolation is very simple, as long as we give the initial and end states and time, you can look at the API yourself.


      Well, with the basis of these three parts, we can start today's content. Today's script is divided into two parts. The first part is the character control part, which is mainly responsible for the character's movement, turning and animation processing in the scene. The second part is the part of camera control, mainly related to camera rotation and camera zoom. Next, let’s talk about these two parts separately. The scene is still a small game:


The protagonist this time is Xie Cangxing, a character everyone likes very much. Alright, let’s go back to today’s content! In the first part, the main thing is to complete the character’s turn in all directions. Here we define four directions (in fact, the eight directions are the same!), the script is as follows:

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NoLockiVew_Player : MonoBehaviour {  
  5.   
  6.     /*Role control in free view*/  
  7.     /*Author: Qin Yuanpei*/  
  8.   
  9.     //player's walking speed  
  10.     public float WalkSpeed=1.5F;  
  11.     //gravity  
  12.     public float Gravity=20;  
  13.   
  14.     //role controller  
  15.     private CharacterController mController;  
  16.     //animation component  
  17.     private Animation mAnim;  
  18.     //Player direction, default forward  
  19.     private DirectionType mType=DirectionType.Direction_Forward;  
  20.   
  21.     [HideInInspector]  
  22.     //Player state, the default is Idle  
  23.     public PlayerState State=PlayerState.Idle;  
  24.   
  25.     //Define the player's state enumeration  
  26.     public enum PlayerState  
  27.     {  
  28.         Idle,  
  29.         Walk  
  30.     }  
  31.   
  32.     //Define the enumeration values ​​of the four directions, calculated in the counterclockwise direction  
  33.     protected enum DirectionType  
  34.     {  
  35.         Direction_Forward=90,  
  36.         Direction_Backward=270,  
  37.         Direction_Left=180,  
  38.         Direction_Right=0  
  39.     }  
  40.       
  41.     void Start ()   
  42.     {  
  43.        // get character controller  
  44.        mController=GetComponent<CharacterController>();  
  45.        //Get the animation component  
  46.        mAnim=GetComponentInChildren<Animation>();  
  47.     }  
  48.       
  49.       
  50.     void Update ()   
  51.     {  
  52.         MoveManager();  
  53.         //MouseEvent();  
  54.     }  
  55.   
  56.     //player movement control  
  57.     void MoveManager()  
  58.     {  
  59.         //moving direction  
  60.         Vector3 mDir=Vector3.zero;  
  61.         if(mController.isGrounded)  
  62.         {  
  63.             //Rotate the character to the corresponding direction  
  64.             if(Input.GetAxis("Vertical")==1)  
  65.             {  
  66.                 SetDirection(DirectionType.Direction_Forward);  
  67.                 mDir=Vector3.forward * Time.deltaTime * WalkSpeed;  
  68.                 mAnim.CrossFade("Walk",0.25F);  
  69.                 State=PlayerState.Walk;  
  70.             }  
  71.             if(Input.GetAxis("Vertical")==-1)  
  72.             {  
  73.                 SetDirection(DirectionType.Direction_Backward);  
  74.                 mDir=Vector3.forward * Time.deltaTime * WalkSpeed;  
  75.                 mAnim.CrossFade("Walk",0.25F);  
  76.                 State=PlayerState.Walk;  
  77.             }  
  78.             if(Input.GetAxis("Horizontal")==-1)  
  79.             {  
  80.                 SetDirection(DirectionType.Direction_Left);  
  81.                 mDir=Vector3.forward * Time.deltaTime * WalkSpeed;  
  82.                 mAnim.CrossFade("Walk",0.25F);  
  83.                 State=PlayerState.Walk;  
  84.             }  
  85.             if(Input.GetAxis("Horizontal")==1)  
  86.             {  
  87.                 SetDirection(DirectionType.Direction_Right);  
  88.                 mDir=Vector3.forward * Time.deltaTime * WalkSpeed;  
  89.                 mAnim.CrossFade("Walk",0.25F);  
  90.                 State=PlayerState.Walk;  
  91.             }  
  92.             //Idle animation of the character  
  93.             if(Input.GetAxis("Vertical")==0 && Input.GetAxis("Horizontal")==0)  
  94.             {  
  95.                 mAnim.CrossFade("Idle",0.25F);  
  96.                 State=PlayerState.Idle;  
  97.             }  
  98.   
  99.         }  
  100.         //Consider the gravity factor  
  101.         mDir=transform.TransformDirection(mDir);  
  102.         float y=mDir.y-Gravity *Time.deltaTime;  
  103.         mDir= new  Vector3(mDir.x,y,mDir.z);  
  104.         mController.Move(mDir);  
  105.     }  
  106.   
  107.     //Set the direction of the character, there is a problem  
  108.     void SetDirection(DirectionType mDir)  
  109.     {  
  110.         if(mType!=mDir)  
  111.         {  
  112.             transform.Rotate(Vector3.up*(mType-mDir));  
  113.             mType=mDir;  
  114.         }  
  115.     }  
  116. }  

Guess you like

Origin blog.csdn.net/hackdjh/article/details/39893959