三、简单的游戏代码
在写代码之前,首先我们需要一个模型进行用例的测试来用。
在Hierarchy视图中添加一个Cube和一个Plane,并为Cube添加上刚体(Rigidbody保证物体的重力作用)。
为Cube创建一个子物体Cylinder,做为Cube方向的标识。
1.鼠标的点击控制代码
在Project->Assets中添加一个文件夹Cs做为代码存储空间,在Cs中建立一个C#脚本“moveCube”,将脚本挂载到Cube上。
在脚本打开后,可见两个自带两个函数void Start()与void Update()作用分别为,游戏运行时函数运行一次和游戏运行中每帧运行一次函数。
在此我们添加一个函数OnMouseDown()//鼠标点击物体后运行函数。
~颜色变化。
添加代码:
void OnMouseDown()
{
//物体.获取<Renderer>属性.材质.颜色=颜色.黑色
this.GetComponent<Renderer>().material.color = Color.black;
}
在点击挂载脚本的物体之后,将物体的材质变为黑色。我们可以发现Cube的颜色变为了黑色,但是Cube的子物体并没有改变。这是因为在脚本中并没有子物体的属性,也就无法进行改变了。
如果希望Cylinder一起改变颜色,我们需要先在脚本中设置一个public GameObject变量,然后再Unity中将Cylinder赋值给他。
代码:
public class moveCube : MonoBehaviour {
public GameObject objCylinder;
void OnMouseDown()
{
//RGB三原色和透明度(支持透明Material->Rendering Mode<Transparent>)
this.GetComponent<Renderer>().material.color = new Color(0.7f, 0.3f, 0.4f, 0.5f);
Color col = new Color(Random.Range(0.1f, 1.0f), Random.Range(0.1f, 1.0f), Random.Range(0.1f, 1.0f), Random.Range(0.1f, 1.0f));
this.GetComponent<Renderer>().material.color = col;
objCylinder.GetComponent<Renderer>().material.color = col;
}
其中Random.Range(float,float)表示取随机值。~坐标变换
代码能够改变的属性当然不只有物体的颜色,如代码的世界坐标也是能改变的。
代码:
public class moveCube : MonoBehaviour {
//三维变量
Vector3 v3 = new Vector3(10, 10, 10);
public GameObject objCylinder;
void OnMouseDown()
{
//物体.坐标.位置=v3;
this.transform.position = v3;
}
}
同样的我们也可以通过GameObject赋值别的物体,然后通过点击Cube改变被赋值的物体。
public class moveCube : MonoBehaviour {
//三维变量
Vector3 v3 = new Vector3(10, 10, 10);
public GameObject objSphere;
public GameObject objCylinder;
void OnMouseDown()
{
//物体.坐标.位置=v3;
objSphere.transform.position = v3;
}
}
在场景中添加一个Sphere将其赋值到objSphere上。~尺寸变换
public class moveCube : MonoBehaviour {
//三维变量
Vector3 v3 = new Vector3(10, 10, 10);
public GameObject objCylinder;
void OnMouseDown()
{
//物体.坐标.位置=v3;
this.transform.localScale = v3;
}
}
物体体积的变化。(因为有刚体存在,所以受力弹开)
2.键位的控制代码
在游戏场景中,经常会需要用到键盘上下左右或WSAD按键控制角色的移动,接下到我们就开始写关于物体移动的代码。
void Update () {
//deltaTime一帧的时间
//mul速度的倍数
int mulTrans = 10, mulRotate = 100, mulAddForce = 200;
//如果(输入.键值(s))
//沿x轴平移使用前后即(back\forward)
if (Input.GetKey(KeyCode.S))
this.transform.Translate(Vector3.back * Time.deltaTime * mulTrans);
//根据世界坐标系移动
//this.transform.Translate(Vector3.back * Time.deltaTime * mulTrans, Space.World);
if (Input.GetKey(KeyCode.W))
this.transform.Translate(Vector3.forward * Time.deltaTime * mulTrans);
//沿着y轴旋转所以使用上下即(up\down)
if (Input.GetKey(KeyCode.A))
this.transform.Rotate(Vector3.up * Time.deltaTime * -mulRotate);
if (Input.GetKey(KeyCode.D))
this.transform.Rotate(Vector3.down * Time.deltaTime * -mulRotate);
}
因为是根据世界坐标系变化,所以物体的旋转实际是对物体进行y轴的旋转。~复原和变色
同样我们也可以通过键位来控制物体的位置和颜色。
if (Input.GetKey(KeyCode.Q))
this.transform.position = v0;
if(Input.GetKey(KeyCode.E))
{
Color col=new Color(Random.Range(0.1f,1.0f),Random.Range(0.1f,1.0f),Random.Range(0.1f,1.0f),Random.Range(0.1f,1.0f));
this.GetComponent<Renderer>().material.color = col;
objCylinder.GetComponent<Renderer>().material.color = col;
}
v0设置为初始坐标。~物体的跳跃
在游戏中,第一人称或第三人称视角的角色,大多都会有跳跃的功能。
//物体.获取刚体.添加沿y轴的力*mulAddForce
if (Input.GetKeyDown(KeyCode.Space))
this.GetComponent<Rigidbody>().AddForce(Vector3.up * mulAddForce);
在这条代码下,在点击空格后物体会得到一个向上的力,后向上运动,但是物体的力是直接获得的,也就是说物体在空中依旧能够继续跳跃,而为了模仿真实场景,我们便需要给他一个限制条件。在物体的碰撞器没有接触时,也就是物体没有与地面接触后,取消物体的跳跃功能。
public class moveCube : MonoBehaviour {
//Cube碰撞器状态
bool cCube = false;
void Update () {
//setCubeChangeColor();
//deltaTime一帧的时间
//mul速度的倍数
int mulAddForce = 200;
//点击结束(点击空格)
//物体.获取刚体.添加沿y轴的力*mulAddForce
if (Input.GetKeyDown(KeyCode.Space)&&cCube)
this.GetComponent<Rigidbody>().AddForce(Vector3.up * mulAddForce);
}
}
//物体碰撞器接触结束(自带)
void OnCollisionExit(Collision collision)
{
cCube = false;
}
//物体碰撞器持续接触(自带)
void OnCollisionStay(Collision collision)
{
cCube = true;
}
~鼠标键的控制
我们在添加一个物体Capsule将Sphere设置为他的子物体,然后设置通过鼠标控制他的移动。
//获取鼠标键0,1,2|左,右,中,Down放开键
if (Input.GetMouseButton(0))
objCapsule.transform.Translate(Vector3.back * Time.deltaTime * mulTrans);
if (Input.GetMouseButton(1))
objCapsule.transform.Rotate(Vector3.up * Time.deltaTime * -mulRotate);
if (Input.GetMouseButtonDown(2))
objCapsule.transform.localScale = v1;
同样在设置代码前,我们需要设置GameObject变量,将objCapsule赋值。