Unity-BattleStar丨10. 第九章U3D代码注解

2024-08-14 16:38:25 浏览数 (2)

Animation.isPlaying

Description

Are we playing any animations?

我们在播放动画吗?

代码语言:javascript复制
 if (!gunAnimation.isPlaying)
        {
            gunAnimation.Play(gunAnimationName);
        }

Animation.Play

Description

Plays an animation without any blending.

If no name is supplied then the default animation will be played. In cases where the animation can't be played (ie, there is no default animation or no animation with the specified name), the function will return false.

代码语言:javascript复制
 if (!gunAnimation.isPlaying)
        {
            gunAnimation.Play(gunAnimationName);
        }

AnimationClip.length

Description

Animation length in seconds. (Read Only)

动画长度的秒数

ParticleSystem.Play()

Description

Starts the particle system.

播放粒子特效

代码语言:javascript复制
//开火粒子特效播放
gunParticle.Play ();

Camera.ScreenPointToRay

Description

Returns a ray going from camera through a screen point.

Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen (position.z is ignored). Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth -1,pixelHeight -1).

此方法的作用是可以从Camera的近视口nearClip向前发射一条射线到屏幕上的position点。参考点position用实际像素值的方式来决定Ray到屏幕的位置。参考点position的X轴分量或Y轴分量从0增长到最大值时,Ray从屏幕一边移动到另一边。当Ray未能碰撞到物体时hit.point返回值为Vector3(0,0,0)。参考点position的Z轴分量值无效。

代码语言:javascript复制
Ray ray = mainCamera.ScreenPointToRay(point);

Input.GetMouseButton

Description

Returns whether the given mouse button is held down.

button values are 0 for left button, 1 for right button, 2 for the middle button.

代码语言:javascript复制
   if (Input.GetMouseButton(0))
            {
                if (activeFire)
                {
                    Fire();
                }
            }

yield return null

暂缓一帧,下一帧执行

Example1

Example2

代码语言:javascript复制
    private IEnumerator GunFire()
    {
        while (true)
        {
            if (Input.GetMouseButton(0))
            {
                if (activeFire)
                {
                    Fire();
                }
            }
            yield return null;
        }
    }

yield return new WaitForSeconds

Example2

代码语言:javascript复制
private IEnumerator GunReloadAnimation()
    {
        if (!gunAnimation.isPlaying)
        {
            gunAnimation.Play("Reload01");

            yield return new WaitForSeconds(gunAnimation.GetClip("Reload01").length);

            gunAnimation.Play("Reload02");

            yield return new WaitForSeconds(gunAnimation.GetClip("Reload02").length);

            ActiveFire = true;
        }
    }

Input.GetKeyDown

代码语言:javascript复制
 if (Input.GetKeyDown(KeyCode.R))
            {
                ReloadBullets();
            }
代码语言:javascript复制
if (Input.GetKeyDown("space"))
            print("space key was pressed");

String.Contains

返回一个值,该值指示指定的子串是否出现在此字符串中。

代码语言:javascript复制
if (hit.transform.name.Contains("Robot"))
                    hit.transform.GetComponent<Robot>().RobotGetHurt();

Physics.RaycastHit

Description

Structure used to get information back from a raycast.

注意:我们不能直接用hit.方法,而是附上它的组件,再跟方法

代码语言:javascript复制
if (hit.transform.name.Contains("Robot"))
                    hit.transform.GetComponent<Robot>().RobotGetHurt();

PS:Gun代码注解

代码语言:javascript复制
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class S_Gun : MonoBehaviour {

    public Text bulletNumberText;

    //枪支Animation组件
    Animation gunAnimation;

    //主摄像机,用于Raycast射线检测
    Camera mainCamera;

    //开火粒子特效
    public ParticleSystem gunParticle;

    //开火声音
    AudioSource gunAudio;

    //子弹数量属性
    int gunBulletNumber;
    public int GunBulletNumber
    {
        set
        {
            gunBulletNumber = value;
            bulletNumberText.text = gunBulletNumber.ToString();
        }
        get
        {
            return gunBulletNumber;
        }
    }

    //控制开火属性。在没换弹完成前不允许开火。因此设置布尔变量,开完火后立即将允许开枪的变量设置为false,在换弹动画完成前不允许开火
    bool activeFire;
    public bool ActiveFire
    {
        set
        {
            activeFire = value;
        }
        get
        {
            return activeFire;
        }
    }

    private void Start()
    {
        gunAnimation = GetComponent<Animation>();
        mainCamera = GameObject.Find("FirstPersonCharacter").GetComponent<Camera>();
        gunAudio = GetComponent<AudioSource>();

        GunBulletNumber = 25;
        ActiveFire = true;

        StartCoroutine(GunFire());
        StartCoroutine(GunReLoad());
    }

    IEnumerator GunFire()
    {
        while (true)
        {
            if (Input.GetMouseButton(0))
            {
                if (activeFire)
                    Fire();
            }
            yield return null;    //一帧怎么能执行两次开火动画呢?两次开火之间要有一个时间差。但这儿即使不隔一帧也没关系,因为我们已设置了开火一次后延迟换弹时间才能进行下一次开火 
        }
    }

    void Fire()
    {
        if (GunBulletNumber > 0)
        {
            activeFire = false;
            GunBulletNumber--;
            Invoke("ResumeFire", gunAnimation.GetClip("Fire01").length);
            ChangeGunAnimation("Fire01");
            gunParticle.Play();
            gunAudio.Play();    //先将GunFire音频置于AudioClip,取消Play On Awake,后续声音切换重新编程

            //发射射线检测是否打中机器人,是则调用机器人减血等动画
            Vector3 point = new Vector3(mainCamera.pixelWidth / 2, mainCamera.pixelHeight / 2,0);
            Ray ray = mainCamera.ScreenPointToRay(point);
            RaycastHit hit;

            if(Physics.Raycast(ray,out hit))
            {
                if (hit.transform.name.Contains("Robot"))
                    hit.transform.GetComponent<Robot>().RobotGetHurt();
            }
        }
    }

    void ResumeFire()
    {
        ActiveFire = true;
    }

    void ChangeGunAnimation(string gunAnimationName)
    {
        if (!gunAnimation.isPlaying)
            gunAnimation.Play(gunAnimationName);   //Animation的名字是string类型,Animation组件会直接调用内部这个名字的Animation动画
    }

    IEnumerator GunReLoad()
    {
        while (true)
        {
            if (Input.GetKeyDown(KeyCode.R))
                ReLoadBullet();
            yield return null;
        }
    }

    void ReLoadBullet()
    {
        if (GunBulletNumber < 25)
        {
            if (!gunAnimation.isPlaying)
            {
                if (activeFire == true)
                {
                    activeFire = false;
                    //gunAnimation.Play("Reload01");
                    Invoke("Reload02", gunAnimation.GetClip("Reload01").length);   //第二种实现换弹方式

                    StartCoroutine(GunReloadAnimation());
                }
            }
        }
    }

    //void Reload02()
    //{
    //    gunAnimation.Play("Reload02");
    //    Invoke("ResumeFire02", gunAnimation.GetClip("Reload02").length);
    //}

    //void ResumeFire02()
    //{
    //    GunBulletNumber = 25;
    //    ActiveFire = true;
    //}

    IEnumerator GunReloadAnimation()
    {
        if (!gunAnimation.isPlaying)
        {
            gunAnimation.Play("Reload01");
            yield return new WaitForSeconds(gunAnimation.GetClip("Reload01").length);
            gunAnimation.Play("Reload02");
            yield return new WaitForSeconds(gunAnimation.GetClip("Reload02").length);

            GunBulletNumber = 25;
            ActiveFire = true;
        }
    }
}

0 人点赞