为了账号安全,请及时绑定邮箱和手机立即绑定

请问代码错在哪里啊,找不到!?

请问代码错在哪里啊,找不到!?

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 关注
  • 128 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信