12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2891|回复: 11
打印 上一主题 下一主题

子弹效果实现5.2---射击游戏系列教程之五

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-22 16:07:00 |只看该作者 |倒序浏览




           以上这段代码主要是运用四元数来确定子弹的角度,由鼠标的位置决定。下面是代码的详细讲解,直接看英文吧...
         

           This code might be a bit tricky to understand but I hope you can bear with me. I wrote a tutorial about
         

           Quaternions that would help explain this code you are welcome to view it here:
         


            http://www.unitytutorials.com/document/280/understanding-quaternions-in-unity-3d
           


           The first line is getting the inputRotation Vector, from the player's position to the mouse. By
         

           multiplying the inputRotation Vector by the Quaternion.AngleAxis instruction I am effectively rotating
         

           the vector by 8 degrees. I do this because I want to create the bullet slightly in front of the player, and
         

           because the gun is slightly to the side I need to find the position which is the tip of the gun and I rotate
         

           8 degrees to the side to find it. Alternatively you could attach an empty game object and make it a
         

           child to your player, move it so it sits around the gun nozzle, create a public game object variable in the
         

           player script and Instantiate all your bullets at this vector:
         

           objGunNozzle.transform.position
         

           Then as you move the gun nozzle object moves as well because it is a child of your player.
         

           The second line of code says, get my position (transform.position), and add to it the direction that
         

           tempVector is now pointing (8 degrees off from my initial position), and place it at 0.8 units from the
         

           origin of the player, this position is the gun nozzel's position. Much simpler if you did it the first way
         

           but it's a great exercise in understanding how you can rotate vectors.
           

           切换到游戏模式下,单击左键,子弹并未发射出去,并与角色发生碰撞,下面继续完善代码。
         

           Go ahead and run your game, run around and left click to create bullets. Right now the bullets don't
         

           move and they collide with the player. Easy fix!
         

           Find this line of code you just added:
         

           Instantiate(ptrScriptVariable.objBullet, tempVector,
         

           Quaternion.LookRotation(inputRotation) ); // create a bullet, and
         

           rotate it based on the vector inputRotation
         

           And replace it as follows:
         

           GameObject objCreatedBullet = (GameObject)
         

           Instantiate(ptrScriptVariable.objBullet, tempVector,
         

           Quaternion.LookRotation(inputRotation) ); // create a bullet, and
         

           rotate it based on the vector inputRotation
         

           Physics.IgnoreCollision(objCreatedBullet.collider, collider);
         

           Great you can build walls of bullets to stop that alien from getting to you! 'GameObject
         

           objCreatedBullet = ' is the same as writing 'Float X = 1f' etc. Except when we call the instantiate
         

           function, we must also specify the type of object being created because this is C# and that is why you
         

           see the (GameObject) before the Instantiate function otherwise the instruction would not work.
           

           下面的代码来实现子弹的运动,声明速度,生命周期等变量。
         

           Let's get our bullets moving now, make the following changes to your BulletScript:
         

           private float moveSpeed = 30f; // how fast the bullet moves
         

           private float timeSpentAlive; // how long the bullet has stayed alive for
         

           private GameObject objPlayer;
         

           // Use this for initialization
         

           void Start () {
         

           objPlayer = (GameObject) GameObject.FindWithTag ("layer");
         

           }
         

           // Update is called once per frame
         

           void Update () {
         

           timeSpentAlive += Time.deltaTime;
         

           if (timeSpentAlive > 1) // if we have been traveling for more than one
         

           second remove the bullet
         

           {
         

           removeMe();
         

           }
         

           // move the bullet
         

           transform.Translate(0, 0, moveSpeed * Time.deltaTime);
         

           transform.position = new Vector3(transform.position.x,
         

           0,transform.position.z); // because the bullet has a rigid body we don't want it
         

           moving off it's Y axis
         

           }
         

           void removeMe ()
         

           {
         

           Destroy(gameObject);
         

           }
         

           void OnCollisionEnter(Collision Other)
         

           {
         

           if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null &&
         

           Other.gameObject != objPlayer ) // if we have hit a character and it is not the
         

           player
         

           {
         

           removeMe();
         

           }
         

           removeMe(); // remove the bullet if it has hit something else apart from
         

           an enemy character
         

           }
         

           Go ahead and see if your code works, if all is successful you should be running around shooting
         

           bullets!
         

           Let's see how we did this. Firstly we are counting the number of seconds our bullet has spent alive by
         

           incrementing timeSpentAlive by Time.deltaTime. If the bullet has been alive for more than one second
         

           we remove it. We do this to stop the bullet traveling forever.
         

           transform.Translate is a great function. Each frame the bullet will move the same distance forward
         

           regardless of what it collides with. We multiply moveSpeed by Time.deltaTime to make sure the
         

           distance it moves is relative to the framerate. For example, if we were running at 30 frames a second
         

           and moved it 1 unit every frame, in 1 second our object would have moved 30 units. But if the game
         

           was running at 10 frames per second it would only move 10 units in 1 second. This is why we multiply
         

           it by Time.deltaTime so that regardless of the framerate our object will always move the same amount
         

           each frame.
         

           The removeMe() function simply destroys the gameObject. The OnCollisionEnter function is a trigger
         

           event that works for any object that owns a collider, whenever the collider (our bullet's box collider in
         

           this case) hits another collider, it will call this function and the function will give us the name of the
         

           collision we hit, in this case we are calling it Other. And we retrieve the gameObject by using
         

           Other.gameObject. This way we can see if the game object we hit has the AIscript (if it does we know
         

           we have hit a character), and we check to see if it is not the player, and now we remove the bullet. In
         

           our next tutorial we will spawn particle effects when the bullet hits objects, apply damage to the alien,
         

           create a death particle effect for the alien, and allow the player to take damage from the alien's melee
         

           attack.
         

           Don't Forget to save your scene Goto > File > Save Scene.
           

            
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-13 23:18:32 |只看该作者
真不错,全存下来了.
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-4-14 23:25:13 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

地板
发表于 2012-5-1 23:22:02 |只看该作者
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-6-28 23:30:27 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-8-26 23:52:26 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-10-27 23:28:26 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2013-2-1 23:30:33 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2013-2-8 09:45:49 |只看该作者
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2013-2-11 23:26:38 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-24 12:27 , Processed in 0.134175 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部