[Unity3D]Unity3D官方案例SpaceShooter开发教程

这篇博客详细介绍了如何使用Unity3D开发一款名为SpaceShooter的游戏。从导入资源、设置环境,到实现玩家飞船控制、光照、背景、子弹发射及边界限制,再到陨石生成、碰撞检测、分数系统和游戏结束条件,逐步构建了一个完整的3D太空射击游戏。同时,文章还分享了关键代码和技巧,包括玩家和敌人运动控制、碰撞响应和游戏逻辑等。
摘要由CSDN通过智能技术生成
1、先导入SpaceShooter的环境资源,将Models下的vehicle_playerShip放到Hierachy窗口,改名为Player.Rotation x改为270,为Player添加mesh collider组件,将vehicle_playerShip_collider放到Mesh属性里面,为Player添加刚体,去掉重力的勾选,将Prefebs中的engines_player放到Hierarchy,并调好位置。
2、进入3D视图,将MainCamera中的Projection改为Orthographic,摄像机视图变为矩形视图。将Camered的背景改为黑色。将Player的Rotation x 改为零。Camera的Rotation改为90,移动摄像机,调节位置在Game视图中查看。通过调节Camera的Size来调节Player的大小,通过调节Camera的Position来调节Player的位置,适当即可。
3、添加一个Directional Light,改名为Main Light,然后reset,改Rotation x为20,y为245.Intensity改为0.75,同理继续添加一个,改名为Fill Light,将Rotation y改为125,点开color将RGB分别改为128,192,192.在添加一个Rim Light,将Rotation x,y分别改为345,65,Indensity改为0.25,Ctrl+Shift+N创建一个GameObject,改名为Light,将三个Light放进去。
4、添加一个Quad组件,改名为Background,rotation x 90,添加一个纹理tile_nebula_green_dff,将tile_nebula_green_dff中的shader改为Unlity/Texture,将Position y 改为-10,将Scale x, y改为15和30,这是纹理图片的大小比例决定的,1:2。
5、创建GameObject,改名为GameCotroller,添加脚本GameCotroller.cs,为Player添加一个新的脚本,命名为PlayerController,脚本为:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
   
    public float zMin = -3.3f;
    public float zMax = 14f;
    public float xMin = -6.5f;
    public float xMax = 6.5f;
}
public class PlayerController : MonoBehaviour {
    public float speed = 10f;
    public Boundary bound;
    void FixedUpdate()
    {
       
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 move = new Vector3(h, 0, v);
        rigidbody.velocity = speed * move;
        rigidbody.position = new Vector3(Mathf.Clamp(rigidbody.position.x, bound.xMin, bound.xMax), 0, Mathf.Clamp(rigidbody.position.z, bound.zMin, bound.zMax));
    }
}
代码心得:限制需要在Game视图移动来调节。记住了rigidbody的velocity方法,限制Player的飞行范围可通过rigidbody.position来控制。学习了Mathf.Clamp(rigidbody.position.x, bound.xMin, bound.xMax)的用法。学习了系列化[System.Serializable]在窗口视图中显示。
6、创建一个GameObject,改名为Bolt,创建一个GameObject,改名为VFX,放到Bolt下,将fx_lazer_orange_dff拖到VFX Inspector中,将Shader改为Particles/Additive可将背景设置为透明。给Bolt加上RigidBody,去掉use Gravity.添加Mover脚本:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
    public float speed = 20f;
 // Use this for initialization
 void Start () {
        rigidbody.velocity = transform.forward * speed;
 }
}
再添加一个Capsule Collider用于碰撞检测,将Radius和height的值设为0.05,0.55.direction设为Z-Axis,现在报错,是因为FBX的MeshRenderer和Bolt的Capsule Collider冲突,删掉FBX的MeshRenderer。注意一点要想把改变的同步到Prefabs中就得点击应用。
开始添加子弹,创建新的GameObject,命名为SpawnPos放到Player下面。继续编辑PlayerController脚本。代码如下:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
   
    public float zMin = -3.3f;
    public float zMax = 14f;
    public float xMin = -6.5f;
    public float xMax = 6.5f;
}
public class PlayerController : MonoBehaviour {
    public float speed = 10f;
   
    public Boundary bound;
    public GameObject bolt;
    public Transform spawnPos;
    void Update()
    {
        if(Input.GetButton("Fire1"))
        {
            Instantiate(bolt, spawnPos.position, spawnPos.rotation);
        }
    }
    void FixedUpdate()
    {
       
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 move = new Vector3(h, 0, v);
        rigidbody.velocity = speed * move;
        rigidbody.position = new Vector3(Mathf.Clamp(rigidbody.position.x, bound.xMin, bound.xMax), 0, Mathf.Clamp(rigidbody.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值