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:
- using UnityEngine;
- using System.Collections;
- public class example : MonoBehaviour {
- //horizontal speed
- public float HorizontalSpeed = 2.0F;
- //vertical speed
- public float VerticalSpeed = 2.0F;
- void Update()
- {
- //horizontal direction
- float h = HorizontalSpeed * Input.GetAxis("Mouse X");
- //Vertical direction
- float v = VerticalSpeed * Input.GetAxis("Mouse Y");
- //rotate
- transform.Rotate(v, h, 0);
- }
- }
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:
- float mY = 5.0;
- void Update ()
- {
- mY += Input.GetAxis("Horizontal");
- transform.eulerAngles =new Vector3(0,mY, 0);
- }
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:
- void Rotating (float horizontal, float vertical)
- {
- // Create a new vector of the horizontal and vertical inputs.
- Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
- // Create a rotation based on this new vector assuming that up is the global y axis.
- Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
- // Create a rotation that is an increment closer to the target rotation from the player's rotation.
- Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
- // Change the players rotation to this new rotation.
- rigidbody.MoveRotation(newRotation);
- }
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:
- using UnityEngine;
- using System.Collections;
- public class NoLockiVew_Player : MonoBehaviour {
- /*Role control in free view*/
- /*Author: Qin Yuanpei*/
- //player's walking speed
- public float WalkSpeed=1.5F;
- //gravity
- public float Gravity=20;
- //role controller
- private CharacterController mController;
- //animation component
- private Animation mAnim;
- //Player direction, default forward
- private DirectionType mType=DirectionType.Direction_Forward;
- [HideInInspector]
- //Player state, the default is Idle
- public PlayerState State=PlayerState.Idle;
- //Define the player's state enumeration
- public enum PlayerState
- {
- Idle,
- Walk
- }
- //Define the enumeration values of the four directions, calculated in the counterclockwise direction
- protected enum DirectionType
- {
- Direction_Forward=90,
- Direction_Backward=270,
- Direction_Left=180,
- Direction_Right=0
- }
- void Start ()
- {
- // get character controller
- mController=GetComponent<CharacterController>();
- //Get the animation component
- mAnim=GetComponentInChildren<Animation>();
- }
- void Update ()
- {
- MoveManager();
- //MouseEvent();
- }
- //player movement control
- void MoveManager()
- {
- //moving direction
- Vector3 mDir=Vector3.zero;
- if(mController.isGrounded)
- {
- //Rotate the character to the corresponding direction
- if(Input.GetAxis("Vertical")==1)
- {
- SetDirection(DirectionType.Direction_Forward);
- mDir=Vector3.forward * Time.deltaTime * WalkSpeed;
- mAnim.CrossFade("Walk",0.25F);
- State=PlayerState.Walk;
- }
- if(Input.GetAxis("Vertical")==-1)
- {
- SetDirection(DirectionType.Direction_Backward);
- mDir=Vector3.forward * Time.deltaTime * WalkSpeed;
- mAnim.CrossFade("Walk",0.25F);
- State=PlayerState.Walk;
- }
- if(Input.GetAxis("Horizontal")==-1)
- {
- SetDirection(DirectionType.Direction_Left);
- mDir=Vector3.forward * Time.deltaTime * WalkSpeed;
- mAnim.CrossFade("Walk",0.25F);
- State=PlayerState.Walk;
- }
- if(Input.GetAxis("Horizontal")==1)
- {
- SetDirection(DirectionType.Direction_Right);
- mDir=Vector3.forward * Time.deltaTime * WalkSpeed;
- mAnim.CrossFade("Walk",0.25F);
- State=PlayerState.Walk;
- }
- //Idle animation of the character
- if(Input.GetAxis("Vertical")==0 && Input.GetAxis("Horizontal")==0)
- {
- mAnim.CrossFade("Idle",0.25F);
- State=PlayerState.Idle;
- }
- }
- //Consider the gravity factor
- mDir=transform.TransformDirection(mDir);
- float y=mDir.y-Gravity *Time.deltaTime;
- mDir= new Vector3(mDir.x,y,mDir.z);
- mController.Move(mDir);
- }
- //Set the direction of the character, there is a problem
- void SetDirection(DirectionType mDir)
- {
- if(mType!=mDir)
- {
- transform.Rotate(Vector3.up*(mType-mDir));
- mType=mDir;
- }
- }
- }