首先制作一款打飞机类的游戏,首先要有控制飞机的代码。
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary{
public float xMin,xMax,zMin,zMax;<span style="color:#999999;">//边界的值</span>
}
public class _PlayerController : MonoBehaviour {
public float speed;
public Boundary boundary;
public float tilt;
public float tilt1;
public GameObject shot;
public Transform shotSpown;
private float nextFire;
public float fireRate;
void Update(){
if (<span style="color:#999999;">/*Input.GetButton ("Fire1") && */</span>Time.time > nextFire) { //被注释掉的那段代码的意思是点击鼠标射击,注释掉了就是自动射击了
nextFire = Time.time + fireRate;
Instantiate (shot, shotSpown.position, shotSpown.rotation);// 实例化一个射击的子弹效果
audio.Play ();//播放射击的音乐
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");//获取水平轴上的方向
float moveVertical = Input.GetAxis ("Vertical");//获取垂直轴上的方向
Vector3 movement=new Vector3 (moveHorizontal, 0.0f, moveVertical);//因为是2D游戏俯视的视角所以Y轴是0.0F
rigidbody.velocity = movement*speed;
rigidbody.position = new Vector3(
Mathf.Clamp(rigidbody.position.x,boundary.xMin,boundary.xMax),//飞船在X轴运动时的边界控制
0.0f,
Mathf.Clamp(rigidbody.position.z,boundary.zMin,boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.z*tilt1, 0.0f, rigidbody.velocity.x*-tilt);//飞船在移动的时候有个偏转的效果
}
}