随发展趋势,初步涉及了一下Unity 3D 知识,以前只是听说过,感觉很高大上的样子。从网上学习了一下,感觉比较容易上手(相对而言,当然这里只是说基础内容↖(^ω^)↗) 。
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)