Unity3D 初涉门径之物体移动(W,A,S,D)实现
2017-02-27 23:38
549 查看
随发展趋势,初步涉及了一下Unity 3D 知识,以前只是听说过,感觉很高大上的样子。从网上学习了一下,感觉比较容易上手(相对而言,当然这里只是说基础内容↖(^ω^)↗) 。
Unity 3D 键盘(W,A,S,D)实现物体移动
代码如下:
C#脚本(Unity 5.5.1 运行):
JS:
嗯,代码先整理出来。
Unity 3D 键盘(W,A,S,D)实现物体移动
代码如下:
C#脚本(Unity 5.5.1 运行):
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test : MonoBehaviour { // 定义全局 变量 public int speed = 5; public Transform bullet; // Use this for initialization void Start () { } // Update is called once per frame void Update () { // -- 移动 功能 // 根据 玩家按下 W,A,S,D 键盘键 实现移动 float x = Input.GetAxis ("Horizontal") * Time.deltaTime * speed; float z = Input.GetAxis ("Vertical") * Time.deltaTime * speed; transform.Translate (x, 0, z); print ("x:"+x); // --开火功能 ,判断玩家是否按下鼠标左键 或 左Ctrl 键 if (Input.GetButtonDown ("Fire1")) { // 实例化 子弹 Transform n = Instantiate(bullet, transform.position, transform.rotation); // 子弹 发射方向 Vector3 front = transform.TransformDirection(Vector3.forward); // 给子弹添加力 n.GetComponent<Rigidbody>().AddForce(front * 2800); // 这是 Unity 5.0 以后 获取 rigidbody 写法 } // -- 旋转 功能 // 以 Y 轴旋转, 实现摄像机左右 转向 if (Input.GetKey(KeyCode.Q)) { /** * arg0: X 轴 * arg1: Y 轴 * arg2: Z 轴 * arg3: 坐标轴系, 以自身坐标为轴 */ transform.Rotate (0, -25 * Time.deltaTime, 0, Space.Self); } if (Input.GetKey (KeyCode.E)) { transform.Rotate (0, 25 * Time.deltaTime, 0, Space.Self); } // 以 X 轴旋转, 实现摄像机上下 转向 if (Input.GetKey (KeyCode.Z)) { transform.Rotate (-25 * Time.deltaTime, 0, 0, Space.Self); } if (Input.GetKey (KeyCode.C)) { transform.Rotate (25 * Time.deltaTime, 0, 0, Space.Self); } // 升高 摄像机(视觉) 功能 if (Input.GetKey(KeyCode.H)) { transform.Translate (0, 5 * Time.deltaTime, 0); } if (Input.GetKey (KeyCode.N)) { transform.Translate (0, -5 * Time.deltaTime, 0); } } }
JS:
#pragma strict function Start () { } var speed:int = 5; var bullet:Transform; function Update () { var x:float = Input.GetAxis("Horizontal") * Time.deltaTime * speed; var z:float = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(x, 0, z); print("shoot:"+x); if(Input.GetButtonDown("Fire1") || Input.GetKey(KeyCode.Space)){ // 实例化 子弹 var n:Transform = Instantiate(bullet, transform.position, transform.rotation); var front:Vector3 = transform.TransformDirection(Vector3.forward); // 射击方向 (旋转方向) // 给子弹 添加力 n.GetComponent<Rigidbody>().AddForce(front*2800); } // 代码和 C# 没什么不同,最大区别就在于 变量的命名。(C# 有命名空间!!!) // -- 旋转 功能 // 以 Y 轴旋转, 实现摄像机左右 转向 if (Input.GetKey(KeyCode.Q)) { /** * arg0: X 轴 * arg1: Y 轴 * arg2: Z 轴 * arg3: 坐标轴系, 以自身坐标为轴 */ transform.Rotate (0, -25 * Time.deltaTime, 0, Space.Self); } if (Input.GetKey (KeyCode.E)) { transform.Rotate (0, 25 * Time.deltaTime, 0, Space.Self); } // 以 X 轴旋转, 实现摄像机上下 转向 if (Input.GetKey (KeyCode.Z)) { transform.Rotate (-25 * Time.deltaTime, 0, 0, Space.Self); } if (Input.GetKey (KeyCode.C)) { transform.Rotate (25 * Time.deltaTime, 0, 0, Space.Self); } // 升高 摄像机(视觉) 功能 if (Input.GetKey(KeyCode.H)) { transform.Translate (0, 5 * Time.deltaTime, 0); } if (Input.GetKey (KeyCode.N)) { transform.Translate (0, -5 * Time.deltaTime, 0); } }
嗯,代码先整理出来。
相关文章推荐
- unity3D 实现NGUI物体朝鼠标点击位置移动
- Unity3d实现Android滑动屏幕移动3D物体
- Unity3D里实现物体移动到目标点,并且摄像机跟着移动的简单方案
- Unity3D里实现物体移动到目标点,并且摄像机跟着移动的简单方案
- 007-实现Unity3d中触摸屏幕控制物体的移动 代码实现
- unity3d 中实现物体的移动
- 原创]unity3D学习【功能实现】之一:坐标的转换以及物体的移动
- Unity3D里实现物体移动到目标点,并且摄像机跟着移动的简单方案
- Unity3d 简单实现物体旋转缩放移动
- Unity3D之实现点击地形使物体移动到指定位置
- 在Unity3D中实现点击地面物体移动到指定地点
- unity3d实现鼠标左键点击地形使角色移动到指定地点
- Unity3d物体模型(实现旋转缩放平移自动旋转)
- 关于unity3D人物鼠标移动(初步实现)
- Unity3D如何实现点击物体替换贴图。
- unity3d 随机函数 场景中物体移动和旋转
- Unity3d 实现鼠标左键点击地形使角色移动到指定地点[脚本]
- [unity3d]如何实现游戏对象跟随鼠标方向移动
- AS3实现沿着圆弧移动的物体
- Unity3D 控制物体移动、旋转、缩放