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

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

2018-02-12 00:56 204 查看
用户输入-Unity3D游戏开发培训

作者:邓家海

时间:2018-02-1214: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

Entity3DAPI

点击Help->UnityManual



图3-1

会在浏览器打开API文档.



图3-2



图3-3

新建两个场景.File->newSence



图3-4

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

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



图3-5

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

usingUnityEngine;

usingSystem.Collections;

///作者:邓家海

///用户:DengJiaHai

///创建日期:2017-01-1022:50:52

///修改:

///版本:V1.0.0.0

//添加菜单名字

[AddComponentMenu("Demo2/InputTest1")]

publicclassInputTest:MonoBehaviour{

//Usethisforinitialization

voidStart(){

}

//Updateiscalledonceperframe

voidUpdate(){

if(Input.GetButton("Fire1"))

{

print("button");

}

if(Input.GetButtonDown("Fire1"))

{

print("down");

}

if(Input.GetButtonUp("Fire1"))

{

print("up");

}

}

}


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



图3-6



图3-7

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

调试一下



图3-8

vector3



图3-9



图3-10



图3-11

usingUnityEngine;
usingSystem.Collections;

publicclassExampleClass:MonoBehaviour{
voidSlide(Transformtarget,Vector3railDirection){
Vector3heading=target.position-transform.position;
Vector3force=Vector3.Project(heading,railDirection);
GetComponent<Rigidbody>().AddForce(force);
}
}




图3-12

usingUnityEngine;
usingSystem.Collections;

publicclassExampleClass:MonoBehaviour{
publicTransformstartMarker;
publicTransformendMarker;
publicfloatspeed=1.0F;
privatefloatstartTime;
privatefloatjourneyLength;
voidStart(){
startTime=Time.time;
journeyLength=Vector3.Distance(startMarker.position,endMarker.position);
}
voidUpdate(){
floatdistCovered=(Time.time-startTime)*speed;
floatfracJourney=distCovered/journeyLength;
transform.position=Vector3.Lerp(startMarker.position,endMarker.position,fracJourney);
}
}




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