[Unity3D] Unity3D game development-effect of four-angle color control based on the legend of xianjian and Qixia

Source: Internet
Author: User

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei.

In the previous article, we developed a non-linear small map from the camera prototype provided by Unity3D. If we use the GUI to draw some background maps under this small map, we believe the overall effect will be better. The blogger hopes that you can do more in-depth research on this issue by yourself, because we have already mentioned the Paster in the previous article, so I am not going to talk about it here. Today, we continue to add some interesting elements to this small project. First, let's take a look at the following picture:


I believe that my friends who are familiar with Chinese standalone games will be familiar with this picture. In the first article in this series, I have mentioned that the blogger is a fan of Chinese standalone games, bloggers like such games with connotation and depth. In terms of operation, the Return System of the xianjian series lags far behind the current real-time system, but I think there is no difference between the return system and the real-time system in essence, real-time conversion is a combination of no limit on the number of attacks. Therefore, in terms of gameplay, players need to cultivate each role in a balanced manner and find the optimal strategy in the battle to give full play to the advantages of each role, therefore, the bloggers believe that if they turn the real-time system into a martial arts system, the turn-based system can be called wendou to some extent. It is precisely because of this that the xianjian series focuses on plots and stories, it has brought countless changes to players. In view of the quality of domestic online game players, bloggers have always been disgusted with online games, so they prefer martial arts/Xianxia single-host games. Although xianjian also launched the online version, however, the online gaming environment with speakers, voices, and mutual abuse in the game makes me unable to find the Fairy sword. Now, let's talk about the role control in Qixia Chuan. Those who have played the legend of the legend, the company has previously developed works such as xianjian Qixia Chuan 3 and xianjian Qixia Chuan 3. Later, due to some reasons, the company was forced to disband. The company was later a cutting edge in a standalone game in China-the ancient sword of Shanghai candle dragon technology. There are many stories that we don't want to believe in the ending, or we don't want to admit it when we see the ending. The blue-colored shirts and the white-haired Murong Ziying on the qingting peak will follow the Dark Blue Sword of the magic sword, we all remember that maybe he really went to tianyao city, just for one sentence: Cheng Jun, this promise, will keep his life. Now, we have officially started technology sharing (there are many things in the blog )!

In the legend of the legend, when you press the back key, the role moves toward the back (Backword). When you press the left button and right-click the button, the role rotates for 90 degrees to the left and right. Strictly speaking, xianjian 4 is not a complete 3D game, because the game's perspective is locked, so players usually do not see the front of the role when running the map. What we need to do today is to implement such a role Controller Based on Unity3D. Although Unity3D provides us with the first-person role controller and the third-person role controller, the blogger feels strange to use the third-person role controller officially provided, especially when the left and right keys are pressed, the rotation is difficult to control, so the blogger decides to write a role controller by himself. First, we open the project. We still use the example of yesterday:

Many friends may think that the role-controlled script is very easy to write. This is a common version we see:

// Left if (Input. getKey (KeyCode. a) {SetAnimation (LeftAnim); this. mState = PersonState. walk; mHero. transform. translate (Vector3.right * Time. deltaTime * mSpeed);} // right if (Input. getKey (KeyCode. d) {SetAnimation (RightAnim); this. mState = PersonState. walk; mHero. transform. translate (Vector3.right * Time. deltaTime * (-mSpeed);} // up if (Input. getKey (KeyCode. w) {SetAnimation (UpAnim); this. mState = PersonState. walk; mHero. transform. translate (Vector3.forward * Time. deltaTime * (-mSpeed);} // downward if (Input. getKey (KeyCode. s) {SetAnimation (DownAnim); this. mState = PersonState. walk; mHero. transform. translate (Vector3.forward * Time. deltaTime * (mSpeed); Vector3 mHeroPos = mHero. transform. position ;}
So, let's just think it's okay to write it like this. Now we import the Script resource package officially provided, find the MouseLook Script, and add the right-click or press judgment in the Update () method, in this way, the angle of view can be rotated by right-clicking. The modified script is as follows:

using UnityEngine;using System.Collections;/// MouseLook rotates the transform based on the mouse delta./// Minimum and Maximum values can be used to constrain the possible rotation/// To make an FPS style character:/// - Create a capsule./// - Add the MouseLook script to the capsule.///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)/// - Add FPSInputController script to the capsule///   -> A CharacterMotor and a CharacterController component will be automatically added./// - Create a camera. Make the camera a child of the capsule. Reset it's transform./// - Add a MouseLook script to the camera.///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)[AddComponentMenu("Camera-Control/Mouse Look")]public class MouseLook : MonoBehaviour {public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }public RotationAxes axes = RotationAxes.MouseXAndY;public float sensitivityX = 15F;public float sensitivityY = 15F;public float minimumX = -360F;public float maximumX = 360F;public float minimumY = -60F;public float maximumY = 60F;float rotationY = 0F;void Update (){if(Input.GetMouseButton(1)){  if (axes == RotationAxes.MouseXAndY)  {float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;rotationY += Input.GetAxis("Mouse Y") * sensitivityY;rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);  }  else if (axes == RotationAxes.MouseX)  {transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);  }  else  {rotationY += Input.GetAxis("Mouse Y") * sensitivityY;rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);  }}}void Start (){// Make the rigid body not change rotationif (rigidbody)rigidbody.freezeRotation = true;}}
Next, we dragged the script to our role to run the game. We found a problem: when the rotation angle is reached, the role did not move in the forward direction as we expected, on the contrary, the role still moves forward along the Vector3.forward in the world coordinate system. According to our ideas, when the angle of view is rotated, the role should be able to move forward. What should we do? Here we add the following code in the above Code:

// Calculate the rotation angle if (Input. getMouseButton (1) {// calculate the horizontal rotation angle mAngles + = Input. getAxis ("Mouse X") * 15; // rotate the role transform. rotation = Quaternion. euler (new Vector3 (0, mAngles, 0 ));}

The purpose of the code here is to calculate the horizontal rotation angle when you right-click the user to rotate the angle of view, and then let the coordinate system of the role rotate along with the angle of view, this is equivalent to parallel Vector3.forward to the Target angle after rotation. In this way, when we control the forward movement of a character, it will follow this new direction. In this way, our first problem is solved. Let's continue to look at it. As this model only provides a walking/running direction animation, there is a problem that the role animation and the role behavior do not match. What should we do? At this time, we can think like this, we can first rotate the role to the specified direction, and then let the role move in the forward direction, so that the role animation and role behavior can correspond to each other. To do this, we will do the following:

// Role action direction enumeration public enum PersonDirection {// normal Forward = 90, // normal Backward = 270, // Normal Left = 180, // normal Right = 0 ,}
Here we define the angle in four directions. When our role is rotated to the Forward direction, we determine the direction to which the role is to be rotated based on the key pressed by the user:

Private void SetPersonDirection (PersonDirection mDir) {// rotate the role if (mDirection! = MDir) {transform. Rotate (Vector3.up * (mDirection-mDir); mDirection = mDir ;}}
In this method, if the target direction is greater than the current direction, the role rotates counter-clockwise. Otherwise, the rotation is clockwise and the angle difference is 0.

Now that the role has been rotated to the corresponding direction, let's move it forward:

transform.Translate(Vector3.forward * WalkSpeed * Time.deltaTime);
Next, we will define an enumeration value for the role's status:

// Role status enumeration public enum PersonState {idle, run, walk, jump, attack}
We modified the above Code and the final code is:

Using UnityEngine; using System. collections; public class RPGControl: MonoBehaviour {// defines the role Animation private Animation mAnimation; // defines the role state public PersonState mState = PersonState. idle; // defines the direction state public PersonDirection mDirection = PersonDirection. forward; // define the bounce volume of the role public float mJumpValue = 2F; // define the Rotation Angle private float mAngles; // define the camera public GameObject mCamera; // define the role action mode public PersonState RunOrWalk = PersonState. walk; public fl Oat worker speed = 1.5F; public float RunSpeed = 3.0F; // enumeration of role statuses public enum PersonState {idle, run, walk, jump, attack} // role action direction enumeration public enum PersonDirection {// normal Forward = 90, // normal Backward = 270, // Normal Left = 180, // normal Right = 0,} void Start () {// obtain the animation mAnimation = gameObject. getComponent <Animation> ();} void Update () {// forward if (Input. getKey (KeyCode. w) {SetPersonDirection (PersonDirection. forward); SetPersonAnimation ();} // Return if (Input. getKey (KeyCode. s) {SetPersonDirection (PersonDirection. backward); SetPersonAnimation ();} // left if (Input. getKey (KeyCode. a) {SetPersonDirection (PersonDirection. left); SetPersonAnimation ();} // right if (Input. getKey (KeyCode. d) {SetPersonDirection (PersonDirection. right); SetPersonAnimation ();} // patrol or wait for if (Input. getKeyUp (KeyCode. a) | Input. getKeyUp (KeyCode. d) | Input. getKeyUp (KeyCode. s) | | Input. getKeyUp (KeyCode. w) | Input. getKeyUp (KeyCode. space) {mAnimation. play ("idle"); mState = PersonState. idle;} // skip if (Input. getKey (KeyCode. space) {transform. getComponent <Rigidbody> (). addForce (Vector3.up * mJumpValue, ForceMode. force); mAnimation. play ("Jump"); mState = PersonState. jump;} // attack if (Input. getMouseButton (0) {mAnimation. play ("Attack"); mState = PersonState. attack; StartCoroutine ("ReSe TState ");} // calculates the rotation angle if (Input. getMouseButton (1) {// calculate the horizontal rotation angle mAngles + = Input. getAxis ("Mouse X") * 15; // rotate the role transform. rotation = Quaternion. euler (new Vector3 (0, mAngles, 0) ;}} private void SetPersonDirection (PersonDirection mDir) {// rotate the role if (mDirection! = MDir) {transform. rotate (Vector3.up * (mDirection-mDir); mDirection = mDir;} private void SetPersonAnimation () {if (RunOrWalk = PersonState. walk) {mAnimation. play ("Walk"); mState = PersonState. walk; transform. translate (Vector3.forward * slow speed * Time. deltaTime);} else if (RunOrWalk = PersonState. run) {mAnimation. play ("Run"); mState = PersonState. run; transform. translate (Vector3.forward * RunSpeed * Time. deltaTime) ;}} IEnumerator ReSetState () {// The yield return new WaitForSeconds (mAnimation. clip. length); mAnimation. play ("idle"); mState = PersonState. idle ;}}
The SetPersonAnimation () method determines whether the role moves by walking or running based on the RunOrWalk value. Finally, we add the Script SmoothFollow following the camera. This Script is in the Script resource package provided by the official website. We bind the Script to the main camera, and set our role to follow the goal.

Finally, let's take a look at the effect Animation: Download the animation from here (the size limit of 2 MB, and the hard-to-use editor)

If you need a project file, you can leave a message!

Like me, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei!

Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/23709427


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.