UNITY3D学习笔记14



using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	Transform m_transform;

	Player m_player;

	NavMeshAgent m_agent;

	float m_movSpeed = 0.5f;

	// Use this for initialization
	void Start () {
	
		m_transform = this.transform;

		m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

		m_agent = GetComponent<NavMeshAgent>();

		m_agent.SetDestination (m_player.m_tranform.position);
	}

	void MoveTo ()
	{
		float speed = m_movSpeed * Time.deltaTime;

		m_agent.Move(m_transform.TransformDirection((new Vector3 (0, 0, speed))));
	}
	
	// Update is called once per frame
	void Update () {
		MoveTo();
	}
}



using UnityEngine;
using System.Collections;

[AddComponentMenu("Game/Spawn")]
public class Spawn : MonoBehaviour
{

    public Transform m_transform;

    public Transform m_enemy;

    void Awake()
    {
        m_transform = this.transform;
    }



    // 图标显示
    void OnDrawGizmos()
    {
        Gizmos.DrawIcon(transform.position, "Node.tif");
    }


}



using UnityEngine;
using System.Collections;

[AddComponentMenu("Game/Player")]
public class Player : MonoBehaviour {

    // 组件
    public Transform m_transform;
    CharacterController m_ch;


    // 角色移动速度
    float m_movSpeed = 3.0f;

    // 重力
    float m_gravity = 2.0f;


    // 摄像机
    Transform m_camTransform;

    // 摄像机旋转角度
    Vector3 m_camRot;

    // 摄像机高度
    float m_camHeight = 1.4f;

    // 生命值
    public int m_life = 5;

    //枪口transform
    Transform m_muzzlepoint;

    // 射击时,射线能射到的碰撞层
    public LayerMask m_layer;

    // 射中目标后的粒子效果
    public Transform m_fx;

    // 射击音效
    public AudioClip m_audio;

    // 射击间隔时间计时器
    float m_shootTimer = 0;


	// Use this for initialization
	void Start () {

        // 获取组件
        m_transform = this.transform;
        m_ch = this.GetComponent<CharacterController>();

        // 获取摄像机
        m_camTransform = Camera.main.transform;

        // 设置摄像机初始位置
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
        m_camTransform.rotation = m_transform.rotation;

        m_camRot = m_camTransform.eulerAngles;

        Screen.lockCursor = true;

        // 查找muzzlepoint
        m_muzzlepoint = m_camTransform.FindChild("M16/weapon/muzzlepoint").transform;
	
	}
	
	// Update is called once per frame
	void Update () {

        // 如果生命为0,什么也不做
        if (m_life <= 0)
            return;

        Control();
	}

    void Control()
    {
       
        //获取鼠标移动距离
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");

        // 旋转摄像机
        m_camRot.x -= rv;
        m_camRot.y += rh;
        m_camTransform.eulerAngles = m_camRot;

        // 使主角的面向方向与摄像机一致
        Vector3 camrot = m_camTransform.eulerAngles;
        camrot.x = 0; camrot.z = 0;
        m_transform.eulerAngles = camrot;

        
        float xm = 0, ym = 0, zm = 0;

        // 重力运动
        ym -= m_gravity*Time.deltaTime;

        // 上下左右运动
        if (Input.GetKey(KeyCode.W)){
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S)){
            zm -= m_movSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A)){
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D)){
            xm += m_movSpeed * Time.deltaTime;
        }

        // 更新射击间隔时间
        m_shootTimer -= Time.deltaTime;

        // 鼠标左键射击
        if (Input.GetMouseButton(0) && m_shootTimer<=0)
        {
            m_shootTimer = 0.1f;

            this.audio.PlayOneShot(m_audio);

            // 减少弹药,更新弹药UI
            GameManager.Instance.SetAmmo(1);

            // RaycastHit用来保存射线的探测结果
            RaycastHit info;

            // 从muzzlepoint的位置,向摄像机面向的正方向射出一根射线
            // 射线只能与m_layer所指定的层碰撞
            bool hit = Physics.Raycast(m_muzzlepoint.position, m_camTransform.TransformDirection(Vector3.forward), out info, 100, m_layer);
            if (hit)
            {
                // 如果射中了tag为enemy的游戏体
                if (info.transform.tag.CompareTo("enemy") == 0)
                {
                    Enemy enemy = info.transform.GetComponent<Enemy>();

                    // 敌人减少生命
                    enemy.OnDamage(1);
                }

                // 在射中的地方释放一个粒子效果
                Instantiate(m_fx, info.point, info.transform.rotation);
            }
        }


        //移动
        m_ch.Move( m_transform.TransformDirection(new Vector3(xm, ym, zm)) );

        // 使摄像机的位置与主角一致
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;


        
    }

    void OnDrawGizmos()
    {
        Gizmos.DrawIcon(this.transform.position, "Spawn.tif");
    }

    // 伤害
    public void OnDamage(int damage)
    {
        m_life -= damage;

        // 更新UI
        GameManager.Instance.SetLife(m_life);

        // 如果生命为0,解锁鼠标显示
        if (m_life <= 0)
            Screen.lockCursor = false;
    }


}



using UnityEngine;
using System.Collections;

[AddComponentMenu("Game/GameManager")]
public class GameManager : MonoBehaviour {

    public static GameManager Instance = null;

    // 游戏得分
    public int m_score = 0;

    // 游戏最高得分
    public static int m_hiscore = 0;

    // 弹药数量
    public int m_ammo = 100;

    // 游戏主角
    Player m_player;

    // UI文字
    GUIText txt_ammo;
    GUIText txt_hiscore;
    GUIText txt_life;
    GUIText txt_score;

	// Use this for initialization
	void Start () {

        Instance = this;

        // 获得主角
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

        // 获得设置的UI文字
        txt_ammo = this.transform.FindChild("txt_ammo").GetComponent<GUIText>();
        txt_hiscore = this.transform.FindChild("txt_hiscore").GetComponent<GUIText>();
        txt_life = this.transform.FindChild("txt_life").GetComponent<GUIText>();
        txt_score = this.transform.FindChild("txt_score").GetComponent<GUIText>();

        txt_hiscore.text = "High Score " + m_hiscore;
	}

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();
    }

    void OnGUI()
    {
        if (m_player.m_life <= 0)
        {
            // 居中显示文字
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;

            // 改变文字大小
            GUI.skin.label.fontSize = 40;

            // 显示Game Over
            GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "Game Over");

            // 显示重新游戏按钮
             GUI.skin.label.fontSize = 30;
            if ( GUI.Button( new Rect( Screen.width*0.5f-150,Screen.height*0.75f,300,40),"Try again"))
            {
                Application.LoadLevel(Application.loadedLevelName);
            }
        }
    }

    // 更新分数
    public void SetScore(int score)
    {
        m_score+= score;

        if (m_score > m_hiscore)
            m_hiscore = m_score;

        txt_score.text = "Score "+m_score;
        txt_hiscore.text = "High Score "+ m_hiscore;
    }

    // 更新弹药
    public void SetAmmo(int ammo)
    {
        m_ammo -= ammo;

        // 如果弹药为负数,重新填弹
        if (m_ammo <= 0)
            m_ammo = 100 - m_ammo;

        txt_ammo.text = m_ammo.ToString()+"/100";
    }

    // 更新生命
    public void SetLife(int life)
    {
        txt_life.text = life.ToString();
    }



}



using UnityEngine;
using System.Collections;

[AddComponentMenu("Game/EnemySpawn")]
public class EnemySpawn : MonoBehaviour
{
    // 敌人的Prefab
    public Transform m_enemy;

    //生成的敌人数量
    public int m_enemyCount = 0;

    // 敌人的最大生成数量
    public int m_maxEnemy = 3;

    // 生成敌人的时间间隔
    public float m_timer = 0;

    protected Transform m_transform;

	// Use this for initialization
	void Start () {

        m_transform = this.transform;

	}
	
	// Update is called once per frame
	void Update () {

        // 如果生成敌人的数量达到最大值,停止生成敌人
        if (m_enemyCount >= m_maxEnemy)
            return;

        // 每间隔一定时间
        m_timer -= Time.deltaTime;
        if (m_timer <= 0)
        {
            m_timer = Random.value * 15.0f;
            if (m_timer < 5)
                m_timer = 5;

            // 生成敌人
            Transform obj=(Transform)Instantiate(m_enemy, m_transform.position, Quaternion.identity);

            // 获取敌人的角本
            Enemy enemy = obj.GetComponent<Enemy>();

            // 初始化敌人
            enemy.Init(this);

        }
	}

    void  OnDrawGizmos () 
    {
        Gizmos.DrawIcon (transform.position, "item.png", true);
    }

}



using UnityEngine;
using System.Collections;


[AddComponentMenu("Game/Enemy")]
public class Enemy : MonoBehaviour {

    // Transform组件
    Transform m_transform;
    //CharacterController m_ch;

    // 动画组件
    Animator m_ani;

    // 寻路组件
    NavMeshAgent m_agent;

    // 主角
    Player m_player;

    // 角色移动速度
    float m_movSpeed = 0.5f;

    // 角色旋转速度
    float m_rotSpeed = 120;

    //  计时器
    float m_timer=2;

    // 生命值
    int m_life = 15;

    // 成生点
    protected EnemySpawn m_spawn;

	// Use this for initialization
	void Start () {

        // 获取组件
        m_transform = this.transform;
        m_ani = this.GetComponent<Animator>();
        m_agent = GetComponent<NavMeshAgent>();

        // 获得主角
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

	}

    // 初始化
    public void Init(EnemySpawn spawn)
    {
        m_spawn = spawn;

        m_spawn.m_enemyCount++;
    }

    // 当被销毁时
    public void OnDeath()
    {
        //更新敌人数量
        m_spawn.m_enemyCount--;

        // 加100分
        GameManager.Instance.SetScore(100);

        // 销毁
        Destroy(this.gameObject);
    }
	
	// Update is called once per frame
	void Update () {

        // 如果主角生命为0,什么也不做
        if (m_player.m_life <= 0)
            return;

        // 获取当前动画状态
        AnimatorStateInfo stateInfo = m_ani.GetCurrentAnimatorStateInfo(0);

        // 如果处于待机状态
        if (stateInfo.nameHash == Animator.StringToHash("Base Layer.idle") && !m_ani.IsInTransition(0))
        {
            m_ani.SetBool("idle", false);

            // 待机一定时间
            m_timer -= Time.deltaTime;
            if (m_timer > 0)
                return;

            // 如果距离主角小于1.5米,进入攻击动画状态
            if (Vector3.Distance(m_transform.position, m_player. m_transform.position) < 1.5f)
            {
                m_ani.SetBool("attack", true);
            }
            else
            {
                // 重置定时器
                m_timer=1;

                // 设置寻路目标点
                m_agent.SetDestination(m_player. m_transform.position);

                // 进入跑步动画状态
                m_ani.SetBool("run", true);
            }
        }

        // 如果处于跑步状态
        if (stateInfo.nameHash == Animator.StringToHash("Base Layer.run") && !m_ani.IsInTransition(0))
        {

            m_ani.SetBool("run", false);


            // 每隔1秒重新定位主角的位置
            m_timer -= Time.deltaTime;
            if (m_timer < 0)
            {
                m_agent.SetDestination(m_player. m_transform.position);

                m_timer = 1;
            }
 
            // 追向主角
            MoveTo();

            // 如果距离主角小于1.5米,向主角攻击
            if (Vector3.Distance(m_transform.position, m_player. m_transform.position) <= 1.5f)
            {
			  //停止寻路	
                m_agent.ResetPath();
		      // 进入攻击状态
                m_ani.SetBool("attack", true);
            }
        }

        // 如果处于攻击状态
        if (stateInfo.nameHash == Animator.StringToHash("Base Layer.attack") && !m_ani.IsInTransition(0))
        {
           
            // 面向主角
            RotateTo();

            m_ani.SetBool("attack", false);

            // 如果攻击动画播完,重新进入待机状态
            if (stateInfo.normalizedTime >= 1.0f)
            {
                m_ani.SetBool("idle", true);

                // 重置计时器
                m_timer = 2;

                m_player.OnDamage(1);
            }
        }

        // 死亡
        if (stateInfo.nameHash == Animator.StringToHash("Base Layer.death") && !m_ani.IsInTransition(0))
        {
            if (stateInfo.normalizedTime >= 1.0f)
            {
                OnDeath();
              
            }
        }

 
	}
   
    // 转向目标点
    void RotateTo()
    {
        // 当前角度   
        Vector3 oldangle = m_transform.eulerAngles;

        //  获得面向主角的角度
        m_transform.LookAt(m_player.m_transform);
        float target = m_transform.eulerAngles.y;

        // 转向主角
        float speed = m_rotSpeed * Time.deltaTime;
        float angle = Mathf.MoveTowardsAngle(oldangle.y, target, speed);
        m_transform.eulerAngles = new Vector3(0, angle, 0);
    }

    // 寻路移动
    void MoveTo()
    {
        float speed = m_movSpeed * Time.deltaTime;
        m_agent.Move(m_transform.TransformDirection((new Vector3(0, 0, speed))));

    }

    // 伤害
    public void OnDamage(int damage)
    {
        m_life -= damage;

        // 如果生命为0,销毁自身
        if (m_life <= 0)
        {
            m_ani.SetBool("death", true);
        }
    }
}



using UnityEngine;
using System.Collections;

[AddComponentMenu("Game/AutoDestroy")]
public class AutoDestroy : MonoBehaviour {

    public float m_timer = 1.0f;

	void Update () {

        m_timer -= Time.deltaTime;
        if (m_timer <= 0)
            Destroy(this.gameObject);
	
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习Unity3D时,以下是一些重要的笔记: 1. Unity3D基础知识: - 游戏对象(Game Objects)和组件(Components):了解游戏对象的层次结构和组件的作用。 - 场景(Scenes)和摄像机(Cameras):学会如何创建场景并设置摄像机视角。 - 材质(Materials)和纹理(Textures):掌握如何创建和应用材质和纹理。 - 动画(Animations):学习如何创建和控制游戏对象的动画。 2. 脚本编程: - C#语言基础:了解C#语言的基本语法和面向对象编程概念。 - Unity脚本编写:学习如何编写脚本来控制游戏对象的行为和交互。 - 常见组件和功能:掌握常见的Unity组件和功能,如碰撞器(Colliders)、刚体(Rigidbodies)、触发器(Triggers)等。 3. 游戏开发流程: - 设计游戏关卡:了解如何设计游戏场景和关卡,包括布局、道具、敌人等。 - 游戏逻辑实现:将游戏规则和玩家交互转化为代码实现。 - UI界面设计:学习如何设计游戏中的用户界面,包括菜单、计分板等。 - 游戏优化和调试:优化游戏性能,解决常见的错误和问题。 4. 学习资源: - Unity官方文档和教程:官方提供了大量的文档和教程,逐步引导你学习Unity3D。 - 在线教程和视频教程:网上有很多免费和付费的Unity教程和视频教程,可根据自己的需求选择学习。 - 社区论坛和博客:加入Unity开发者社区,与其他开发者交流并获取帮助。 通过系统地学习这些内容,你将能够掌握Unity3D的基础知识并开始开发自己的游戏项目。记得不断实践和尝试,不断提升自己的技能!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值