用户输入- Unity3D游戏开发培训

用户输入- Unity3D游戏开发培训

 

作者:Jesai

时间:2018-02-12 14:28:45

用户输入Input

鼠标按键:

-方法:GetMouseButton();

-方法:GetMouseButtonDown()

-方法:GetMouseButtonUp()

-参数为int,0表示左键,1表示右键,2表示滚轮

 

键盘输入

-方法:GetKey()

-方法:GetKeyDown()

-方法:GetKeyUp()

-参数为KeyCode枚举,表示按的某个键

 

控制对象

控制对象的位置、旋转、缩放

位置:属性position,方法Translate()

旋转:方法Rotate() ,方法RotateAround()

缩放:属性localScale

类Vector3:表示三维向量,可以理解为三维空间中的点

-成员up、down、left、fight、forward、back、zero、one

Entity3D API

点击Help->Unity Manual

 

 

图 3-1

 

会在浏览器打开API文档.

 

 

图 3-2

 

 

图 3-3

 

新建两个场景.File->new Sence

 

 

 

图 3-4

 

新建一个空对象create->create Empty,命名script,reset一下Tranform

 

 

新建一个C#脚本,双击打开脚本

 

 

 

图 3-5

 

脚本原始的样子是这样的,什么也没有,只有两个方法:

 

 1 using UnityEngine;
 2 
 3 using System.Collections;
 4 
 5 /// 作者:邓家海
 6 
 7 /// 用户:DengJiaHai
 8 
 9 /// 创建日期:2017-01-10 22:50:52
10 
11 /// 修改:
12 
13 /// 版本:V 1.0.0.0
14 
15 //添加菜单名字
16 
17 [AddComponentMenu("Demo2/InputTest1")]
18 
19 public class InputTest : MonoBehaviour {
20 
21  
22 
23     // Use this for initialization
24 
25     void Start () {
26 
27    
28 
29     }
30 
31    
32 
33     // Update is called once per frame
34 
35     void Update () {
36 
37         if (Input.GetButton("Fire1"))
38 
39         {
40 
41             print("button");
42 
43         }
44 
45         if (Input.GetButtonDown("Fire1"))
46 
47         {
48 
49             print("down");
50 
51         }
52 
53         if (Input.GetButtonUp("Fire1"))
54 
55         {
56 
57             print("up");
58 
59         }
60 
61     }
62 
63 }

 

 

选中新建的script场景,然后Edit->Project Settings ->input,然后设置input的属性。

 

 

 

图 3-6

 

 

 

图 3-7

 

Window->Console  打开控制台,控制台一般用于打印错误消息或者调试。

调试一下

 

 

 

图 3-8

 

 

vector3

 

图 3-9

 

图 3-10

 

 

 

图 3-11

 

 

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 
 5 
 6 public class ExampleClass : MonoBehaviour {
 7     void Slide(Transform target, Vector3 railDirection) {
 8         Vector3 heading = target.position - transform.position;
 9         Vector3 force = Vector3.Project(heading, railDirection);
10         GetComponent<Rigidbody>().AddForce(force);
11     }
12 }

 

 

 

 

图 3-12

 

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 
 5 
 6 public class ExampleClass : MonoBehaviour {
 7     public Transform startMarker;
 8     public Transform endMarker;
 9     public float speed = 1.0F;
10     private float startTime;
11     private float journeyLength;
12     void Start() {
13         startTime = Time.time;
14         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
15     }
16     void Update() {
17         float distCovered = (Time.time - startTime) * speed;
18         float fracJourney = distCovered / journeyLength;
19         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
20     }
21 }

 

 
 

 

posted @ 2018-02-12 00:56  Jesai  阅读(385)  评论(0编辑  收藏  举报