unity3d中2D射击游戏1:cube发射子弹,到一定高度自动销毁
其他系列教程请搜索 "蜜蜂"一、unity3d中可以实现cube发射子弹,到一定高度自动销毁
创建一个新项目,保存场景level1
1.创建cube
2.创建平行光
3.更改摄像头projection-》Oithographic 大小size:5
4.创建一个材质Asset->Material,绑定到cube上
5.调整Main Camera->Background=black
6.新建文件夹Script 新建一个PlayerScript JS脚本 绑定到Cube中
7.添加如下代码var playerSpeed:int;
function Update () {
//amount to move player 移动速度s
amtToMove=(playerSpeed * Input.GetAxis("Horizontal"))*
Time.deltaTime;
// move translate the player水平移动物体
transform.Translate(Vector3.right * amtToMove);
}8.给Cube添加rigidbody在Is Kinematic打上勾
9.设定playerLives 及 全局 soure 分数后 绘制GUI代码 在左上角显示生命值与 分数
10.创建一个Sphere命名为bulletScale大小为0.3 0.3 0.3这是cube发射的子弹
11.Assets->Create ->Material改名为bulletMat更改颜色后绑定到sphere中
12.创建一个控制子弹的js 命名为bulletScript添加var bulletSpeed:int; 调整为10 绑定到sphere
13.添加代码var bulletSpeed:int;function Update () {
//
amtToMove=bulletSpeed * Time.deltaTime;
transform.Translate(Vector3.up * amtToMove);
}14.设置y轴 如果子弹向上到y=6.5的地方销毁
添加如下代码在update中
:if(transform.position.y >=6.5)
{
Destroy(this.gameObject);
}15.创建BulletPrefab 绑定Sphere 删除sphere
16.在playerScript中Update中添加如下代码:var bullet:Rigidbody;
if(Input.GetKeyDown("space")){
var tempBullet:Rigidbody;
tempBullet=Instantiate
(bullet,transform.position,transform.rotation);
}BulletScript的代码如下图:
PlayerScript的代码如下图:
绑定rigidbody 的bulletprefab 即可发射子弹
至此,可以实现cube发射子弹,并到y=6.5自动销毁
如下图:
http://www.u3dchina.com/forum.php?mod=image&aid=2960&size=300x300&key=20ff1937666f77b1&nocache=yes&type=fixnone
源码下载:http://www.u3dchina.com/t-2342-1-1.html 非常不错的很好的入门教程,相信会受益的 amtToMove这个提示无效是怎么回事,新手一个,求指导
感谢楼主的无私分享! 很不错 楼主是超人 好帖就是要顶 顶顶多好 真心顶