Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of how to implement character movement with Unity3D

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to achieve the example of character movement in Unity3D. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

One is to move the character through W, A, S, D (example 1), and the other is to press the button on the screen to move the character (example 2). It's simple, only a few lines of code have been changed.

Here are the resources in the "Assets" folder.

Example 1:

Using System.Collections;using System.Collections.Generic;using UnityEngine; public class E3_07keyboard: MonoBehaviour {/ / Animation array private Object [] animUp; private Object [] animDown; private Object [] animLeft; private Object [] animRight; / / Map private Texture2D map; / / current character animation private Object [] tex; / / character X coordinate private int x; / / character Y coordinate private int y; / / frame sequence private int nowFram; / / Total number of animation frames private int mFrameCount / / limit the number of frames per second private float fps = 5; / / limit the time of frames private float time = 0; void Start () {/ / get all picture resources in the frame animation animUp = Resources.LoadAll ("up"); animDown = Resources.LoadAll ("down"); animLeft = Resources.LoadAll ("left"); animRight = Resources.LoadAll ("right"); / / get map resources map = (Texture2D) Resources.Load ("map/map") / / set default animation tex = animUp;} void OnGUI () {/ / paint GUI.DrawTexture (new Rect (0,0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0); / / draw frame animation DrawAnimation (tex, new Rect (x, y, 32,48)); / / Click the button to move the character if (Input.GetKey (KeyCode.W)) {y-= 2; tex = animUp } if (Input.GetKey (KeyCode.S)) {y + = 2; tex = animDown;} if (Input.GetKey (KeyCode.A)) {x-= 2; tex = animLeft;} if (Input.GetKey (KeyCode.D)) {x + = 2; tex = animRight;}} void DrawAnimation (Object [] tex, Rect rect) {/ / draw the current frame GUI.DrawTexture (rect, (Texture) tex [nowFram], ScaleMode.StretchToFill, true, 0) / / calculate the limit frame time time + = Time.deltaTime; / / switch picture if (time > = 1.0 / fps) {/ / frame sequence switch nowFram++; / / limit frame emptying time = 0; / / the total number of over-frame animations starts from frame 0 (nowFram > = tex.Length) {nowFram = 0;}}

Example two

Using System.Collections;using System.Collections.Generic;using UnityEngine; public class E3_07button: MonoBehaviour {/ / Animation array private Object [] animUp; private Object [] animDown; private Object [] animLeft; private Object [] animRight; / / Map private Texture2D map; / / current character animation private Object [] tex; / / character X coordinate private int x; / / character Y coordinate private int y; / / frame sequence private int nowFram; / / Total number of animation frames private int mFrameCount / / limit the number of frames per second private float fps = 5; / / limit the time of frames private float time = 0; void Start () {/ / get all picture resources in the frame animation animUp = Resources.LoadAll ("up"); animDown = Resources.LoadAll ("down"); animLeft = Resources.LoadAll ("left"); animRight = Resources.LoadAll ("right"); / / get map resources map = (Texture2D) Resources.Load ("map/map") / / set default animation tex = animUp;} void OnGUI () {/ / paint GUI.DrawTexture (new Rect (0,0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0); / / draw frame animation DrawAnimation (tex, new Rect (x, y, 32,48)); / / Click the button to move the character if (GUILayout.RepeatButton (up)) {y-= 2; tex = animUp } if (GUILayout.RepeatButton ("down")) {y + = 2; tex = animDown;} if (GUILayout.RepeatButton ("left")) {x-= 2; tex = animLeft;} if (GUILayout.RepeatButton ("right")) {x + = 2; tex = animRight;}} void DrawAnimation (Object [] tex, Rect rect) {/ / draw the current frame GUI.DrawTexture (rect, (Texture) tex [nowFram], ScaleMode.StretchToFill, true, 0) / / calculate the limit frame time time + = Time.deltaTime; / / switch picture if (time > = 1.0 / fps) {/ / frame sequence switch nowFram++; / / limit frame emptying time = 0; / / the total number of over-frame animations starts from frame 0 (nowFram > = tex.Length) {nowFram = 0;}}

This is the end of the example of how to achieve character mobility in Unity3D. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report