福建省建設(shè)招投標(biāo)網(wǎng)站南昌seo排名公司
文章目錄
- 介紹
- 一,制作玩家具體函數(shù)腳本PlayerCharacter
- 三、 制作玩家控制腳本 PlayerController,調(diào)用上面的函數(shù)方法
- 四、 制作子彈腳本 shell
- 五、 給玩家掛載腳本
- 六、 制作坦克腳本
- 七、 給坦克添加組件
- 八、 開始游戲,播放動(dòng)畫
- 九、 下載
介紹
3d游戲。
玩家自由移動(dòng)。
持槍發(fā)射子彈。
隨機(jī)生成的坦克追蹤玩家。
坦克發(fā)射子彈攻擊玩家。
hp歸零死亡
一,制作玩家具體函數(shù)腳本PlayerCharacter
設(shè)置float:移動(dòng)速度、轉(zhuǎn)動(dòng)速度、子彈發(fā)射速度、現(xiàn)在生命值、最大生命值、兩次攻擊時(shí)間間隔。
設(shè)置bool:是否正在攻擊、是否死亡
其他組件:角色控制器、生命值滑動(dòng)條、最大生命值顏色(綠色)、最小生命值顏色(紅色)、音頻源、動(dòng)畫器、粒子系統(tǒng)
等
public float speed;
public float turnSpeed;
public float health;
public float attackTime;
public float health;
float healthMax;bool isAlive;
bool attacking = false;CharacterController cc;
Animator animator;
public Slider healthSlider;
public Image healthFillImage;
public Color healthColorFull = Color.green;
public Color HealthColorNull = Color.red;public ParticleSystem explosionParticles;
public Rigidbody shell;
public Transform muzzle;
start方法,獲取動(dòng)畫器、角色控制器、生命值最大化、bool設(shè)置活著、更新血條滑塊、死亡爆炸效果設(shè)置為不可見。
animator = GetComponentInChildren<Animator>();cc = GetComponent<CharacterController>();healthMax = health;isAlive = true;RefreshHealthHUD();explosionParticles.gameObject.SetActive(false);
玩家被攻擊,生命值減少amount,更新血條,如果生命值小于零,死亡
public void TakeDamage(float amount){health -= amount;RefreshHealthHUD();if (health <= 0f && isAlive){Death();}}
更新血條,滑塊的value值更新一次,血條由100%的綠色,變成百分之(health / healthMax)的綠色。
public void RefreshHealthHUD(){healthSlider.value = health;healthFillImage.color = Color.Lerp(HealthColorNull, healthColorFull, health / healthMax);}
死亡,角色是否活著設(shè)置為否,播放粒子特效,當(dāng)粒子系統(tǒng)的持續(xù)時(shí)間結(jié)束后,粒子系統(tǒng)對象就會被銷毀,設(shè)置玩家為不可見。
public void Death(){isAlive = false;explosionParticles.transform.parent = null;explosionParticles.gameObject.SetActive(true);ParticleSystem.MainModule mainModule = explosionParticles.main;Destroy(explosionParticles.gameObject, mainModule.duration);gameObject.SetActive(false);}
角色移動(dòng),傳入一個(gè)向量值,必須在活著、沒有攻擊才能移動(dòng)。
角色控制器使用simplemove函數(shù),移動(dòng)的時(shí)候,控制“speed”動(dòng)畫,開始播放
public void Move(Vector3 v){if (!isAlive) return;if (attacking) return;Vector3 movement = v * speed;cc.SimpleMove(movement);if(animator){animator.SetFloat("Speed", cc.velocity.magnitude);}}
人物旋轉(zhuǎn)函數(shù),玩家往哪里跑,角色人物頭就轉(zhuǎn)向哪里
設(shè)置目標(biāo)位置、當(dāng)前位置
四元數(shù)轉(zhuǎn)向
使用球面插值,平滑轉(zhuǎn)動(dòng)
public void Rotate(Vector3 lookDir){var targetPos = transform.position + lookDir;var characterPos = transform.position;//去除Y軸影響characterPos.y = 0;targetPos.y = 0;//角色面朝目標(biāo)的向量Vector3 faceToDir = targetPos - characterPos;//角色面朝目標(biāo)方向的四元數(shù)Quaternion faceToQuat = Quaternion.LookRotation(faceToDir);//球面插值Quaternion slerp = Quaternion.Slerp(transform.rotation, faceToQuat, turnSpeed * Time.deltaTime);transform.rotation = slerp;}
開火腳本
必須活著、不正在開火才能調(diào)用
生成一個(gè)子彈剛體,玩家自身位置,槍口方向轉(zhuǎn)向,設(shè)置發(fā)射速度
播放開火音效
動(dòng)畫器播放開火動(dòng)作
延遲attachtime發(fā)射一次,設(shè)置發(fā)射頻率
public void Fire(){if (!isAlive) return;if (attacking) return;Rigidbody shellInstance = Instantiate(shell, muzzle.position, muzzle.rotation) as Rigidbody;shellInstance.velocity = launchForce * muzzle.forward;shootAudioSource.Play();if(animator){animator.SetTrigger("Attack");}attacking = true;Invoke("RefreshAttack", attackTime);}
發(fā)射時(shí)間間隔腳本,時(shí)間未到,不能發(fā)射
void RefreshAttack(){attacking = false;}
三、 制作玩家控制腳本 PlayerController,調(diào)用上面的函數(shù)方法
start方法獲取角色控制器
void Start (){character = GetComponent<PlayerCharacter>();}
固定幀數(shù)刷新。
鼠標(biāo)左鍵調(diào)用點(diǎn)擊開火函數(shù)
鍵盤wsad控制人物移動(dòng)
人物移動(dòng)的方向和人物轉(zhuǎn)向的方向保持一致
void FixedUpdate(){if (Input.GetButtonDown("Fire1")){character.Fire();}var h = Input.GetAxis("Horizontal");var v = Input.GetAxis("Vertical");character.Move(new Vector3(h, 0, v));var lookDir = Vector3.forward * v + Vector3.right * h;if (lookDir.magnitude != 0){character.Rotate(lookDir);}}
四、 制作子彈腳本 shell
設(shè)置float參數(shù):子彈生存時(shí)間、爆炸半徑、爆炸力量、最大傷害設(shè)置bool參數(shù):子彈是否正在旋轉(zhuǎn)設(shè)置其他:音頻源、層級、粒子系統(tǒng)
public float lifeTimeMax = 2f;public AudioSource explosionAudioSource;public ParticleSystem explosionParticles;public float explosionRadius;public float explosionForce = 1000f;public float damageMax = 100f;public LayerMask damageMask;public bool isRotate = false;
start方法,添加扭矩,模擬子彈旋轉(zhuǎn)
void Start (){if(isRotate){GetComponent<Rigidbody>().AddTorque(transform.right * 1000);}}
觸發(fā)器碰撞檢測
在當(dāng)前游戲?qū)ο蟮奈恢蒙蟿?chuàng)建一個(gè)球形的檢測區(qū)域,并檢測該區(qū)域內(nèi)是否有與指定層級(damageMask)匹配的碰撞器(colliders)。如果有,則返回一個(gè)碰撞器數(shù)組(Collider[]),其中包含了所有與當(dāng)前游戲?qū)ο笤谥付ò霃?#xff08;explosionRadius)內(nèi)發(fā)生碰撞的游戲?qū)ο蟮呐鲎财?。通常用于?shí)現(xiàn)爆炸傷害、碰撞檢測等功能。
遍歷數(shù)組,計(jì)算傷害
播放粒子特效
private void OnTriggerEnter(Collider other){Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius, damageMask);foreach(var collider in colliders){var targetCharacter = collider.GetComponent<PlayerCharacter>();if (targetCharacter){targetCharacter.TakeDamage(CalculateDamage(collider.transform.position));}}explosionParticles.transform.parent = null;explosionAudioSource.Play();explosionParticles.Play();ParticleSystem.MainModule mainModule = explosionParticles.main;// Destroy(explosionParticles.gameObject, mainModule.duration);Destroy(gameObject);}
計(jì)算傷害,傳入向量值
距離越小,傷害比例越大
設(shè)置最低傷害Mathf.Max()函數(shù),最低傷害為2
float CalculateDamage(Vector3 targetPosition){var distance = (targetPosition - transform.position).magnitude;//距離越小,傷害比例越大var damageModify = (explosionRadius - distance) / explosionRadius;var damage = damageModify * damageMax;return Mathf.Max(2f, damage);}
五、 給玩家掛載腳本
設(shè)置參數(shù),添加預(yù)制體子彈
六、 制作坦克腳本
設(shè)置導(dǎo)航網(wǎng)格、play對象
NavMeshAgent agent;PlayerCharacter character;Transform targetTrans;
start函數(shù),獲取玩家實(shí)例,
導(dǎo)航網(wǎng)格尋找玩家
延遲1秒后,每隔三秒發(fā)射一次子彈
void Start (){character = GetComponent<PlayerCharacter>();agent = GetComponent<NavMeshAgent>();targetTrans = GameObject.FindGameObjectWithTag("Player").transform;InvokeRepeating("FireControl", 1, 3);}
開火控制函數(shù),調(diào)用共同使用的fire()方法。
void FireControl(){character.Fire();}
update()函數(shù),不斷追蹤玩家的位置,
轉(zhuǎn)向玩家
void Update (){agent.destination = targetTrans.position;transform.LookAt(targetTrans);}
七、 給坦克添加組件
八、 開始游戲,播放動(dòng)畫
人物移動(dòng)、坦克移動(dòng)
九、 下載
https://pan.baidu.com/s/1RVemZY_THhhpD0v_IcQS2w
提取碼:tdq9
+