亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問代碼錯在哪里啊,找不到???

請問代碼錯在哪里啊,找不到!?

HUWWW 2023-04-24 18:14:25
我用的代碼//var Speed = 50;//var Control;{//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);}else if(Input.GetKey(KeyCode.E));{//Control.RightRotate(Vector3.up *Time.deltaTime * Speed);}}錯在那里呢?
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超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);
}
}

鑒于規范和效率問題,應該是要這樣寫
規范:變量名(var 變量名 : 類型)開頭字母小寫,方法名(function 方法名(){})開頭字母大寫。
效率:聲明變量時應該都定義好變量類型, GetComponent.<js1>();比 GetComponent("js1”);更好
var speed : int = 50; //改成小寫開頭
//
var control : js1; //改成小寫開頭,js1這個類名應該也要大寫開頭才規范

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);
}
}


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號