【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1740|回复: 0

7-5 Enemy Shooting 敌人的射击

[复制链接]
发表于 2013-10-17 13:14:32 | 显示全部楼层 |阅读模式
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class EnemyShooting : MonoBehaviour
  4. {
  5.         public float maximumDamage = 120f;                  // 开枪的最大伤害值.
  6.         public float minimumDamage = 45f;                   // 开枪的最小伤害值.
  7.         public AudioClip shotClip;                          // 开枪声音片段.
  8.         public float flashIntensity = 3f;                   // 枪的火光的强度.
  9.         public float fadeSpeed = 10f;                       // 开枪火光的消失时间.
  10.         
  11.         
  12.         private Animator anim;                              // animator变量.
  13.         private HashIDs hash;                               // 哈希变量.
  14.         private LineRenderer laserShotLine;                 // 枪射击的弹道.
  15.         private Light laserShotLight;                       // 枪发出的火光.
  16.         private SphereCollider col;                         // 包围球.
  17.         private Transform player;                           // 玩家矩阵.
  18.         private PlayerHealth playerHealth;                  // 玩家的血量.
  19.         private bool shooting;                              // 是否处于射击状态.
  20.         private float scaledDamage;                         // 记录开枪击中玩家伤害值.
  21.         
  22.         
  23.         void Awake ()
  24.         {
  25.                 // 初始化变量.
  26.                 anim = GetComponent<Animator>();
  27.                 laserShotLine = GetComponentInChildren<LineRenderer>();
  28.                 laserShotLight = laserShotLine.gameObject.light;
  29.                 col = GetComponent<SphereCollider>();
  30.                 player = GameObject.FindGameObjectWithTag(Tags.player).transform;
  31.                 playerHealth = player.gameObject.GetComponent<PlayerHealth>();
  32.                 hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  33.                
  34.                 // 初始状态弹道处于不可用,枪的火花亮度为0.
  35.                 laserShotLine.enabled = false;
  36.                 laserShotLight.intensity = 0f;
  37.                
  38.                 // 计算出伤害百分比.
  39.                 scaledDamage = maximumDamage - minimumDamage;
  40.         }
  41.         
  42.         
  43.         void Update ()
  44.         {
  45.                 // 从射击动画曲线获取当前射击的状态.
  46.                 float shot = anim.GetFloat(hash.shotFloat);
  47.                
  48.                 // 如果当前的值是射击动作..
  49.                 if(shot > 0.5f && !shooting)
  50.                         // ... 执行射击函数
  51.                         Shoot();
  52.                
  53.                 // 如果是射击的保持动作...
  54.                 if(shot < 0.5f)
  55.                 {
  56.                         // ... 射击标志位为false,弹道设为不可见.
  57.                         shooting = false;
  58.                         laserShotLine.enabled = false;
  59.                 }
  60.                
  61.                 // 射击的花火消失.
  62.                 laserShotLight.intensity = Mathf.Lerp(laserShotLight.intensity, 0f, fadeSpeed * Time.deltaTime);
  63.         }
  64.         
  65.         //---------------------------------------------------
  66.         //  当玩家离机器人很近的时候,机器人转动手臂瞄准玩家射击
  67.         //---------------------------------------------------
  68.         void OnAnimatorIK (int layerIndex)
  69.         {
  70.                 // 从动画曲线获取胳膊的运动状态.
  71.                 float aimWeight = anim.GetFloat(hash.aimWeightFloat);
  72.                
  73.                 // 设置动力学影响的向量----机器人的右手指向玩家,.
  74.                 anim.SetIKPosition(AvatarIKGoal.RightHand, player.position + Vector3.up);
  75.                
  76.                 // 设置当前右手的权重.
  77.                 anim.SetIKPositionWeight(AvatarIKGoal.RightHand, aimWeight);
  78.         }
  79.         
  80.         
  81.         void Shoot ()
  82.         {
  83.                 // 设置射击标志位.
  84.                 shooting = true;
  85.                
  86.                 // 计算玩家到机器人之间的距离.
  87.                 float fractionalDistance = (col.radius - Vector3.Distance(transform.position, player.position)) / col.radius;
  88.                
  89.                 // 根据这个距离求得玩家受到的伤害值.
  90.                 float damage = scaledDamage * fractionalDistance + minimumDamage;
  91.                
  92.                 // 玩家的血量减去这么多的伤害.
  93.                 playerHealth.TakeDamage(damage);
  94.                
  95.                 // 显示开枪的特效.
  96.                 ShotEffects();
  97.         }
  98.         
  99.         
  100.         void ShotEffects ()
  101.         {
  102.                 // 设置弹道射线的起始位置.
  103.                 laserShotLine.SetPosition(0, laserShotLine.transform.position);
  104.                
  105.                 // 设置弹道射线的终点位置.
  106.                 laserShotLine.SetPosition(1, player.position + Vector3.up * 1.5f);
  107.                
  108.                 // 启用弹道.
  109.                 laserShotLine.enabled = true;
  110.                
  111.                 // 设置开枪的火花强度.
  112.                 laserShotLight.intensity = flashIntensity;
  113.                
  114.                 // 播放开枪的声音
  115.                 AudioSource.PlayClipAtPoint(shotClip, laserShotLight.transform.position);
  116.         }
  117. }
复制代码
JS脚本
  1. #pragma strict

  2. public var maximumDamage : float = 120f;                    // The maximum potential damage per shot.
  3. public var minimumDamage : float = 45f;                 // The minimum potential damage per shot.
  4. public var shotClip : AudioClip;                            // An audio clip to play when a shot happens.
  5. public var flashIntensity : float = 3f;                 // The intensity of the light when the shot happens.
  6. public var fadeSpeed : float = 10f;                     // How fast the light will fade after the shot.


  7. private var anim : Animator;                                // Reference to the animator.
  8. private var hash : HashIDs;                             // Reference to the HashIDs script.
  9. private var laserShotLine : LineRenderer;                   // Reference to the laser shot line renderer.
  10. private var laserShotLight : Light;                     // Reference to the laser shot light.
  11. private var col : SphereCollider;                           // Reference to the sphere collider.
  12. private var player : Transform;                         // Reference to the player's transform.
  13. private var playerHealth : PlayerHealth;                    // Reference to the player's health.
  14. private var shooting : boolean;                             // A bool to say whether or not the enemy is currently shooting.
  15. private var scaledDamage : float;                           // Amount of damage that is scaled by the distance from the player.


  16. function Awake ()
  17. {
  18.     // Setting up the references.
  19.     anim = GetComponent(Animator);
  20.     laserShotLine = GetComponentInChildren(LineRenderer);
  21.     laserShotLight = laserShotLine.gameObject.light;
  22.     col = GetComponent(SphereCollider);
  23.     player = GameObject.FindGameObjectWithTag(Tags.player).transform;
  24.     playerHealth = player.gameObject.GetComponent(PlayerHealth);
  25.     hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
  26.    
  27.     // The line renderer and light are off to start.
  28.     laserShotLine.enabled = false;
  29.     laserShotLight.intensity = 0f;
  30.    
  31.     // The scaledDamage is the difference between the maximum and the minimum damage.
  32.     scaledDamage = maximumDamage - minimumDamage;
  33. }


  34. function Update ()
  35. {
  36.     // Cache the current value of the shot curve.
  37.     var shot : float = anim.GetFloat(hash.shotFloat);
  38.    
  39.     // If the shot curve is peaking and the enemy is not currently shooting...
  40.     if(shot > 0.5f && !shooting)
  41.         // ... shoot
  42.         Shoot();
  43.    
  44.     // If the shot curve is no longer peaking...
  45.     if(shot < 0.5f)
  46.     {
  47.         // ... the enemy is no longer shooting and disable the line renderer.
  48.         shooting = false;
  49.         laserShotLine.enabled = false;
  50.     }
  51.    
  52.     // Fade the light out.
  53.     laserShotLight.intensity = Mathf.Lerp(laserShotLight.intensity, 0f, fadeSpeed * Time.deltaTime);
  54. }


  55. function OnAnimatorIK (layerIndex : int)
  56. {
  57.     // Cache the current value of the AimWeight curve.
  58.     var aimWeight : float = anim.GetFloat(hash.aimWeightFloat);
  59.    
  60.     // Set the IK position of the right hand to the player's centre.
  61.     anim.SetIKPosition(AvatarIKGoal.RightHand, player.position + Vector3.up);
  62.    
  63.     // Set the weight of the IK compared to animation to that of the curve.
  64.     anim.SetIKPositionWeight(AvatarIKGoal.RightHand, aimWeight);
  65. }


  66. function Shoot ()
  67. {
  68.     // The enemy is shooting.
  69.     shooting = true;
  70.    
  71.     // The fractional distance from the player, 1 is next to the player, 0 is the player is at the extent of the sphere collider.
  72.     var fractionalDistance : float = (col.radius - Vector3.Distance(transform.position, player.position)) / col.radius;

  73.     // The damage is the scaled damage, scaled by the fractional distance, plus the minimum damage.
  74.     var damage : float = scaledDamage * fractionalDistance + minimumDamage;

  75.     // The player takes damage.
  76.     playerHealth.TakeDamage(damage);
  77.    
  78.     // Display the shot effects.
  79.     ShotEffects();
  80. }


  81. function ShotEffects ()
  82. {
  83.     // Set the initial position of the line renderer to the position of the muzzle.
  84.     laserShotLine.SetPosition(0, laserShotLine.transform.position);
  85.    
  86.     // Set the end position of the player's centre of mass.
  87.     laserShotLine.SetPosition(1, player.position + Vector3.up * 1.5f);
  88.    
  89.     // Turn on the line renderer.
  90.     laserShotLine.enabled = true;
  91.    
  92.     // Make the light flash.
  93.     laserShotLight.intensity = flashIntensity;
  94.    
  95.     // Play the gun shot clip at the position of the muzzle flare.
  96.     AudioSource.PlayClipAtPoint(shotClip, laserShotLight.transform.position);
  97. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-9-28 00:21

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表