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));
}
}
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;
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;
}
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.
再添加一个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.