您的位置:首页 > 移动开发 > Unity3D

Unity3D 初涉门径之物体移动(W,A,S,D)实现

2017-02-27 23:38 549 查看
随发展趋势,初步涉及了一下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);
// 子弹 发射方向
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);
}

}


嗯,代码先整理出来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: