二.設置玩家飛行器,控制玩家移動和限制範圍,製作子彈,控制發射子彈
1.在Models文件夾裏面找到vehicle_playerShip,拉進Scene面板,取名爲Player,位置往上拉5米
2.給player加上Box碰撞器,同時在Player前方新建一個子物體,作爲子彈生成的位置,每次發射子彈,在這裏生成發射
3.然後在文件夾裏找到飛船的引擎的粒子特效,拉到Player下作爲子物體,調整位置,運行遊戲可以看到效果
4.製作子彈預製體:在文件夾裏面找到子彈預製體,然後拉到場景裏,取名爲Bullet,然後拉回我們自己的預製體文件夾:
5.控制飛船移動和移動範圍:新建一個腳本,取名爲:Player
然後腳本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可顯示
public class PlayerClampBounder//移動的X,Z最大範圍或者最小範圍類
{
public float MaxX;
public float MaxZ;
public float MinX;
public float MinZ;
}
public class Player : MonoBehaviour {
public PlayerClampBounder Bounder;//邊界範圍
public float MoveSpeed;//移動速度
void Start () {
}
// Update is called once per frame
void Update () {
MoveMent();
}
public void MoveMent()//控制移動的方法
{
float Horizontal = Input.GetAxis("Horizontal");//獲取水平輸入軸
float Vertical = Input.GetAxis("Vertical");//獲取垂直輸入軸
if (transform.position.x >= Bounder.MaxX && Horizontal > 0)//如果當前x位置大於等於邊界範圍,且還在向這個方向運動,也就是說還按下了向右,就讓水平輸入值爲0,也就不會再繼續向右
{
Horizontal = 0;
}
else if (transform.position.x <= Bounder.MinX && Horizontal < 0)//同理
{
Horizontal = 0;
}
else if (transform.position.z >= Bounder.MaxZ && Vertical > 0)
{
Vertical = 0;
}
else if (transform.position.z <= Bounder.MinZ && Vertical < 0)
{
Vertical = 0;
}
transform.Translate(Horizontal*MoveSpeed*Time.deltaTime, 0, Vertical*Time.deltaTime*MoveSpeed);//移動
}
}
寫完腳本掛到Player下,設置邊界範圍和速度
6.讓子彈飛起來:新建一個腳本Bullet,讓子彈發射出去,代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public float BulletMoveSpeed = 10;
void Start () {
}
// Update is called once per frame
void Update () {
BulletMove();
}
private void BulletMove()//移動的方法
{
transform.Translate(0, 0, BulletMoveSpeed * Time.deltaTime);
}
}
7.發射子彈,在Player腳本里,新建一個方法,代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可顯示
public class PlayerClampBounder//移動的X,Z最大範圍或者最小範圍類
{
public float MaxX;
public float MaxZ;
public float MinX;
public float MinZ;
}
public class Player : MonoBehaviour {
public PlayerClampBounder Bounder;//邊界範圍
public float MoveSpeed;//移動速度
public float ShotDuratime = 0.3f;//發射的間隔時間
public GameObject BulletPrefab;//子彈的預製體,在unity進行賦值
private Transform BulletPoint;//子彈生成的位置
void Start () {
BulletPoint = transform.Find("BulletPoint");//獲取子物體BulletPoint
}
// Update is called once per frame
void Update () {
MoveMent();
ShotBullet();
}
public void MoveMent()//控制移動的方法
{
float Horizontal = Input.GetAxis("Horizontal");//獲取水平輸入軸
float Vertical = Input.GetAxis("Vertical");//獲取垂直輸入軸
if (transform.position.x >= Bounder.MaxX && Horizontal > 0)//如果當前x位置大於等於邊界範圍,且還在向這個方向運動,也就是說還按下了向右,就讓水平輸入值爲0,也就不會再繼續向右
{
Horizontal = 0;
}
else if (transform.position.x <= Bounder.MinX && Horizontal < 0)//同理
{
Horizontal = 0;
}
else if (transform.position.z >= Bounder.MaxZ && Vertical > 0)
{
Vertical = 0;
}
else if (transform.position.z <= Bounder.MinZ && Vertical < 0)
{
Vertical = 0;
}
transform.Translate(Horizontal*MoveSpeed*Time.deltaTime, 0, Vertical*Time.deltaTime*MoveSpeed);//移動
}
private void ShotBullet()
{
ShotDuratime -= Time.deltaTime;
if (ShotDuratime <= 0&&Input.GetMouseButton(0))//當時間到了且按下了鼠標左鍵
{
Instantiate(BulletPrefab, BulletPoint.position, Quaternion.identity);//生成子彈
ShotDuratime = 0.3f;//重新設置間隔時間
}
}
}
進行簡單賦值之後,運行,飛船便可以發射子彈,但是子彈會一直存在於場景內,所以下一節,我們會說到子彈的銷燬。