一、Transform基本移動(dòng)函數(shù)
1. 按照指定方向移動(dòng)一定距離,應(yīng)用范圍:wasd鍵以及上下左右控制類似
float TranslateSpeed = 10f;
//Vector3.forward 表示“向前”
transform.Translate(Vector3.forward *TranslateSpeed);
2. 全方向移動(dòng)一定的距離
//x軸移動(dòng)速度移動(dòng)速度
float xSpeed = -5f;
//z軸移動(dòng)速度移動(dòng)速度
float zSpeed = 10f;
//向x軸移動(dòng)xSpeed,同時(shí)想z軸移動(dòng)zSpeed,y軸不動(dòng)
transform.Translate(xSpeed,0,zSpeed);
3. 重置坐標(biāo),移動(dòng)到世界坐標(biāo)的相對(duì)位置,也就是修改對(duì)應(yīng)position屬性
//x軸坐標(biāo)
float xPostion = -5f;
//z軸坐標(biāo)
float zPostion = 10f;
//直接將當(dāng)前物體移動(dòng)到x軸為xPostion,y軸為0,z軸為zPostion的三維空間位置。
transform.position = new Vector3(xPostion,0,zPostion);
二、輸入控制
1. 鍵盤(pán)控制
GetKey
當(dāng)通過(guò)名稱指定的按鍵被用戶按住時(shí)返回true
GetKeyDown
當(dāng)用戶按下指定名稱的按鍵時(shí)的那一幀返回true。
GetKeyUp
在用戶釋放給定名字的按鍵的那一幀返回true。
GetAxis(“Horizontal")
和GetAxis(“Verical”)
用方向鍵或WASD鍵來(lái)模擬-1到1的平滑輸入
鍵盤(pán)判斷:
if (Input.GetKeyDown (KeyCode.A)) {
//KeyCode表示包含鍵盤(pán)所有鍵
print ("按下A鍵");
}
if (Input.GetKeyUp (KeyCode.D)) {
//當(dāng)按D鍵松開(kāi)時(shí)
print ("松開(kāi)D鍵");
}
if (Input.GetAxis ("Horizontal")) {
//當(dāng)按下水平鍵時(shí)
print ("按下水平鍵");
}
if (Input.GetKeyUp ("Verical")) {
//當(dāng)按下垂直鍵時(shí)
print ("按下垂直鍵");
}
- 持續(xù)按,持續(xù)輸出
//按下鍵盤(pán)“上方向鍵”
if(Input.GetKey ("up"))
print("Up!");
//按下鍵盤(pán)“W鍵”
if(Input.GetKey(KeyCode.W);)
print("W!");
- 一次檢測(cè)(在鍵盤(pán)按下去的時(shí)候)
if(Input.GetKeyDown(KeyCode.Q))
{
print("Q");
}
-
GetAxis
C#鍵盤(pán)控制腳本
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}
2. 鼠標(biāo)控制
GetButton
根據(jù)按鈕名稱返回true當(dāng)對(duì)應(yīng)的虛擬按鈕被按住時(shí)。
GetButtonDown
在給定名稱的虛擬按鈕被按下的那一幀返回true。
GetButtonUp
在用戶釋放指定名稱的虛擬按鈕時(shí)返回true。
if (Input.GetButton ("Fire1")) {
//Fire1表示按下鼠標(biāo)左鍵
print("按下鼠標(biāo)左鍵");
}
if (Input.GetMouseButton(0)) {
//Fire1表示按下鼠標(biāo)左鍵
print("按下鼠標(biāo)左鍵");
}
if (Input.GetMouseButton(1)) {
//1表示按下鼠標(biāo)右鍵
print("按下鼠標(biāo)右鍵");
}
if (Input.GetMouseButton(2)) {
//2表示按下鼠標(biāo)中鍵
print("按下鼠標(biāo)中鍵");
}
-
GetAxis
C#鼠標(biāo)控制腳本
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
按住鼠標(biāo)拖動(dòng)物體旋轉(zhuǎn)和自定義角度旋轉(zhuǎn)物體腳本
float speed = 100.0f;
float x;
float z;
void Update () {
if(Input.GetMouseButton(0)){//鼠標(biāo)按著左鍵移動(dòng)
y = Input.GetAxis("Mouse X") * Time.deltaTime * speed;
x = Input.GetAxis("Mouse Y") * Time.deltaTime * speed;
}else{
x = y = 0 ;
}
//旋轉(zhuǎn)角度(增加)
transform.Rotate(new Vector3(x,y,0));
/**---------------其它旋轉(zhuǎn)方式----------------**/
//transform.Rotate(Vector3.up *Time.deltaTime * speed);//繞Y軸 旋轉(zhuǎn)
//用于平滑旋轉(zhuǎn)至自定義目標(biāo)
pinghuaxuanzhuan();
}
//平滑旋轉(zhuǎn)至自定義角度
void OnGUI(){
if(GUI.Button(Rect(Screen.width - 110,10,100,50),"set Rotation")){
//自定義角度
targetRotation = Quaternion.Euler(45.0f,45.0f,45.0f);
// 直接設(shè)置旋轉(zhuǎn)角度
//transform.rotation = targetRotation;
// 平滑旋轉(zhuǎn)至目標(biāo)角度
iszhuan = true;
}
}
bool iszhuan= false;
Quaternion targetRotation;
void pinghuaxuanzhuan(){
if(iszhuan){
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
}
}
鍵盤(pán)控制物體縮放腳本
float speed = 5.0f;
float x;
float z;
void Update () {
x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; //水平
z = Input.GetAxis("Vertical") * Time.deltaTime * speed; //垂直//"Fire1","Fine2","Fine3"映射到Ctrl,Alt,Cmd鍵和鼠標(biāo)的三鍵或腰桿按鈕。新的輸入軸可以在Input Manager中添加。
transform.localScale += new Vector3(x, 0, z);
/**---------------重新設(shè)置角度(一步到位)----------------**/
//transform.localScale = new Vector3(x, 0, z);
}
其他資料參考:Unity3D研究院之腳本實(shí)現(xiàn)模型的平移與旋轉(zhuǎn)
使用腳本控制模型移動(dòng)旋轉(zhuǎn)與碰撞