学习unity3d一个多月了,也做几个简单demo,但都是跟教程来做的。这次为了找一份实习,下定决心做一个游戏的demo,虽然没什么内容,极易通关,优化也差,各种bug。
But when you being the lowest,whatever you do is up。
游戏名:侏罗纪公园
游戏类型:FPS
故事概述:你突然出现一处充满霸王龙的神秘森林,你必须杀死它们中的头目:巨型霸王龙才能生存下去。
制作历程:
1.作图构思,写预览图
2.根据构图设计UI,场景
3.收集资源,制作地形,场景
4.添加资源,编写代码实现功能
5.第一阶段完成:地形,天空盒,恐龙模型
6.添加代码到相应gameoject实现功能。
暂时不展示成果。
贴出核心代码并记录所遇到的问题。
DianusorAI:
using UnityEngine; using System.Collections; public class DinosaurAI : Unit { public GameObject enemy; private GameObject myenemy; private UnityEngine.AI.NavMeshAgent nam; public float rotateSpeed; // Use this for initialization void Start () { base.Start (); myenemy= Instantiate (enemy, transform.position, transform.rotation)as GameObject; } // Update is called once per frame void Update () { nam=GetComponent<UnityEngine.AI.NavMeshAgent>(); RotateTo (); nam.SetDestination(myenemy.transform.position); } void RotateTo(){ Vector3 targetdir = transform.position - myenemy.transform.position; Vector3 newdir = Vector3.RotateTowards (transform.forward,targetdir,rotateSpeed*Time.deltaTime,0.0f); transform.rotation = Quaternion.LookRotation (newdir); } } shell: using UnityEngine; using System.Collections; public class shell2 : MonoBehaviour { public int damage; public float explosionRadius; public GameObject explosionEffect; public float explosiontime; void OnCollisionEnter(){ GameObject g= Instantiate (explosionEffect, transform.position, transform.rotation)as GameObject; Destroy (gameObject); Destroy (g, explosiontime); Collider[] cols = Physics.OverlapSphere (transform.position,explosionRadius); if (cols.Length > 0) { for(int i=0;i<cols.Length;i++){ Unit u=cols[i].GetComponent<Unit>(); if(u!=null){ u.ApplyDamage(damage); } } } } }shootboom: using UnityEngine; using System.Collections; public class shootboom : MonoBehaviour { public Transform shootPoint; public GameObject shell; public float shootSpeed; private AudioSource audioSource; // Use this for initialization void Start () { audioSource = GetComponent<AudioSource> (); } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { Shoot(); } } public void Shoot(){ GameObject newsheel =Instantiate (shell, shootPoint.position, shootPoint.rotation)as GameObject; Rigidbody r = newsheel.GetComponent<Rigidbody> (); r.velocity = shootPoint.forward * shootSpeed; audioSource.Play (); } }
Unit: using UnityEngine; using System.Collections; public class Unit : MonoBehaviour { public int health=1000; private int curhealth; public GameObject deadEffect; // Use this for initialization public void Start () { this.curhealth = this.health; } public int getcurhea(){ return this.curhealth; } // Update is called once per frame void Update () { } public void ApplyDamage(int damage){ if (curhealth > damage) { curhealth -= damage; } else { Destruct(); } } public void Destruct(){ if(deadEffect!=null){ Instantiate(deadEffect,transform.position,transform.rotation); } Destroy (gameObject); } } welcomeCG: using UnityEngine; using System.Collections; public class Weclomecg : MonoBehaviour { public GameObject world; public GameObject canvas; // Use this for initialization void Start () { StartCoroutine(WaitAndPrint(2.0F)); Destroy (canvas); } // Update is called once per frame void Update () { } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); canvas.SetActive (false); world.SetActive (true); } }
收获:
1.延迟执行可用Invoke 延迟等待执行也可用yield return
void Start () { StartCoroutine(WaitAndPrint(2.0F)); Destroy (canvas); }
IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); canvas.SetActive (false); world.SetActive (true); }
2.在制作游戏过程中,很多单位都会拥有的值可存放在一个Unit基类中,并去继承它。3.实现射击通常是用射线检测。我这里用产生子弹,子弹碰撞到物体会检测一定范围内的所有碰撞体,用数组去获取它们,然后逐个判断是否为敌人,是则造成伤害值。
该方法特别适用于炸弹爆炸。 详情见shell中代码。
4.给某个物体加一个速度。例子:
GameObject newsheel =Instantiate (shell, shootPoint.position, shootPoint.rotation)as GameObject; Rigidbody r = newsheel.GetComponent<Rigidbody> ();
r.velocity = shootPoint.forward * shootSpeed;
5.实现某个物体随机走动或者寻敌。
不寻敌的话可以设置该物体的transform不断改变,使用random函数。
否则首先要在Navigation中烘焙。然后给该物体添加NavMeshAgent。挂上脚本即可。不寻敌的话使用Random即可实现。我在此游戏中创造一个随机物体后setdestinatin
脚本中核心代码(实现寻敌)
void Update () { nam=GetComponent<UnityEngine.AI.NavMeshAgent>(); RotateTo (); nam.SetDestination(myenemy.transform.position); } void RotateTo(){ Vector3 targetdir = transform.position - myenemy.transform.position; Vector3 newdir = Vector3.RotateTowards (transform.forward,targetdir,rotateSpeed*Time.deltaTime,0.0f); transform.rotation = Quaternion.LookRotation (newdir); }
6.让模型播放动画,需在导入unity前给模型配备动画,在用unity中添加AnimationController,并设置状态机。附加到模型上,模型即可播放动画, 7.添加fog效果后,会影响某些特效的渲染。
效果图:
游戏试玩:
百度云盘:http://pan.baidu.com/s/1o8LiLZG
