In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Unity3D how to achieve character shift and movement, in view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
There are a lot of character walking control scripts realized by ray on the Internet, but it is assumed that they only want to be controlled by keyboard keys. For example, the third-person perspective control, in fact, only need to carry out a simple angle transformation. Ideas such as the following:
1. According to the clockwise direction, the front, right, back and left are set as 0pr 1pr 2pr 3, respectively.
2. Set the initial value of the state to 0, that is, facing forward.
3. The previous direction value is subtracted from the current direction value, and the steering angle is multiplied by 90 °, then the rotation transformation can be carried out.
Using UnityEngine;using System.Collections;using System.Linq;public class move: MonoBehaviour {private int State;// role status private int oldState=0;// previous role state private int UP = 0 role status forward private int RIGHT = 1 role status right private int DOWN = 2 role status backward private int LEFT = 3 role status backward public float speed=8 Void Start () {} void Update () {if (Input.GetKey ("w")) {setState (UP);} else if (Input.GetKey ("s")) {setState (DOWN);} if (Input.GetKey ("a")) {setState (LEFT);} else if (Input.GetKey ("d")) {setState (RIGHT);}} void setState (int currState) {Vector3 transformValue = new Vector3 () / / define the translation vector int rotateValue = (currState-State) * 90; transform.animation.Play ("walk"); / / play the character walk animation switch (currState) {case 0displacement / character state forward, the character continues to move forward slowly transformValue = Vector3.forward * Time.deltaTime * speed; break; case 1State / when the character state is to the right. The character continues to move slowly to the right transformValue = Vector3.right * Time.deltaTime * speed; break; case 2 when the character state is backward. The character continues to move slowly backward transformValue = Vector3.back * Time.deltaTime * speed; break; case 3VG / when the character state is to the left, the character continues to move slowly to the left transformValue = Vector3.left * Time.deltaTime * speed; break;} transform.Rotate (Vector3.up, rotateValue); / / rotate the character transform.Translate (transformValue, Space.World); / / translate the character oldState = State;// assignment to facilitate the next calculation State = currState / / assignment to facilitate next calculation}}
This is the answer to the question about how Unity3D can turn and move characters. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.