1 回答

TA貢獻1834條經(jīng)驗 獲得超8個贊
要這樣寫
var Speed = 50;
//
var Control;
function Update(){ //要用Update()方法
//
Control = GetComponent("js1"); //GetComponent()里面填的是字符串類型。
//
if(Input.GetKey(KeyCode.W)) //if(xxx)后面是沒有" ; "的
{
//
Control.ForWard();
}else if(Input.GetKey(KeyCode.S))
{
//
Control.Back();
}
if(Input.GetKey(KeyCode.A))
{
//
Control.GLeft();
}else if(Input.GetKey(KeyCode.D))
{
//
Control.GRight();
}
if(Input.GetKey(KeyCode.Q))
{
//
Control.leftRotate(Vector3.up *Time.deltaTime * -Speed);
}else if(Input.GetKey(KeyCode.E))
{
//
Control.RightRotate(Vector3.up *Time.deltaTime * Speed);
}
}
鑒于規(guī)范和效率問題,應(yīng)該是要這樣寫
規(guī)范:變量名(var 變量名 : 類型)開頭字母小寫,方法名(function 方法名(){})開頭字母大寫。
效率:聲明變量時應(yīng)該都定義好變量類型, GetComponent.<js1>();比 GetComponent("js1”);更好
var speed : int = 50; //改成小寫開頭
//
var control : js1; //改成小寫開頭,js1這個類名應(yīng)該也要大寫開頭才規(guī)范
function Update(){
//
control = GetComponent.<js1>();
//
if(Input.GetKey(KeyCode.W))
{
//
control.ForWard();
}else if(Input.GetKey(KeyCode.S))
{
//
control.Back();
}
if(Input.GetKey(KeyCode.A))
{
//
control.GLeft();
}else if(Input.GetKey(KeyCode.D))
{
//
control.GRight();
}
if(Input.GetKey(KeyCode.Q))
{
//
control.LeftRotate(Vector3.up *Time.deltaTime * -speed); //原來這里的leftRotate我改成大寫了,注意一下
}else if(Input.GetKey(KeyCode.E))
{
//
control.RightRotate(Vector3.up *Time.deltaTime * speed);
}
}