【Unity俯视角射击】我们来做一个《元气骑士》的完整Demo1
---------------------------相關(guān)功能實現(xiàn)--------------------------------
1、人物移動
人物的移動依舊是利用在Update中獲取鍵盤輸入的"Horizontal"和"Vertical"的值,在FixedUpdate中更新角色的位置。
private void Update()
{
???movement.x = Input.GetAxis("Horizontal");
???movement.y = Input.GetAxis("Vertical");
???if (movement == new Vector2(0, 0)){
??????playerAnima.SetBool("run", false);
???}
????else{
????????playerAnima.SetBool("run", true);
???}
}
private void FixedUpdate(){
????rigidbody.MovePosition(rigidbody.position + movement * moveSpeed * Time.fixedDeltaTime);
}
2、人物朝向
人物朝向其實附帶功能還有武器朝向,我寫了一個Target字段現(xiàn)在賦值為鼠標(biāo)位置,有需要也可以修改(比如改為自瞄),考慮到武器種類不同的情況下瞄準(zhǔn)方式也會不一樣,所以人物的朝向和武器的朝向是各自管理的。
3、鼠標(biāo)左鍵操作邏輯
這個按鍵需要判斷四種狀態(tài):一種是玩家在可互動物體周圍時左鍵操作為“互動”,比如現(xiàn)在做的是撿起槍。另外則是長按、短按和抬起,不同武器在長按、短按、抬起這三個狀態(tài)下的操作是不一樣的。
為了判斷玩家是否在可互動物體的周圍,我在Update中每幀由玩家向周圍發(fā)射射線檢測是否碰撞到可互動物體,這里因為只有槍就只做了槍的判斷未進一步優(yōu)化。
weaponInFloor = null;
Collider2D[] cols = Physics2D.OverlapCircleAll(transform.position, 1);
if (cols.Length > 0){
???for (int i = 0; i < cols.Length; i++){
???????if (cols[i].CompareTag("Weapon")){
????????????weaponInFloor = cols[i].gameObject;
???????}
????}
}
根據(jù)射線檢測的結(jié)果和按鍵的情況分別執(zhí)行對應(yīng)的操作:
if (weaponInFloor != null && fireKeyDown){
????GetWeapon();
}
else{
?????if (fireKeyDown){
????????if (weapon != null){ weapon.ShootButtonDown(); }
????????else{ Debug.Log("沒有武器只能手刀撒"); }
?????}
?????else if(fireKeyPressed){
????????if (weapon != null){ weapon.ShootButtonPressed();}
?????}
?????if(fireKeyUp){
????????if (weapon != null){ weapon.ShootButtonUp(); }
?????}
}
GIF
武器的拾取需要根據(jù)玩家手上是否持有武器做不同動作,相應(yīng)的武器身上也要有被撿起放下的兩個對應(yīng)方法:
void GetWeapon(){
????if (weaponInFloor != null){
????????//地上有槍就換槍
????????if (weapon != null){
????????????myWeapon.transform.SetParent(GameManager.instance.weaponRecycle);
????????????weapon.PickDown();
????????}
????????????myWeapon = weaponInFloor;
????????????weapon = myWeapon.GetComponent<Weapon>();
????????????weapon.Pickup();
????????????myWeapon.transform.SetParent(transform);
????????????myWeapon.transform.localPosition = new Vector3(0, 0, 0);
????????????myWeapon.transform.localRotation = Quaternion.identity;
????????????//注冊
????????????weapon.Initialization(gameObject.tag, gameObject.layer);
????}
}
不同武器在對應(yīng)的狀態(tài)方法中編輯該狀態(tài)下的操作即可。
//例:Gun
public override void ShootButtonDown(){
????if (Time.time - timing >=CD){
????????timing = Time.time;
????????GameObject bullet = Instantiate(bulletPrefab, pos.position, pos.rotation*Quaternion.AngleAxis(Random.Range(0,shake),Vector3.forward));
????????bullet.GetComponent<Bullet>().Initialization(attack, role, bulletForce);
????????GetComponent<Animator>().Play("Shoot");
?????}
}
public override void ShootButtonPressed(){
?????if (Time.time - timing >=CD){
???????timing = Time.time;
???????GameObject bullet = Instantiate(bulletPrefab, pos.position, pos.rotation*Quaternion.AngleAxis(Random.Range(0,shake),Vector3.forward));
???????bullet.GetComponent<Bullet>().Initialization(attack, role, bulletForce);
???????GetComponent<Animator>().Play("Shoot");
??????}
}
4、鏡頭跟隨
鏡頭跟隨這塊直接用了Cinemachine,跟著馬老師的視頻設(shè)置就可以啦:
【簡明UNITY教程】不用寫代碼的高級2D攝像機 Cinemachine
三、湊一個小標(biāo)題(有感而發(fā))
由于中間跑去學(xué)ShaderGraph再回頭看俯視角射擊有種恍若隔世的感覺,慢慢找回寫代碼的狀態(tài)思路也更清晰了一些,但其實對自己現(xiàn)在寫的這個Demo并不是太滿意,大概會有漫長的打磨吧。
同時認(rèn)識到以前對俯視角射擊看得太簡單了,又或者說其實我寫的確實是俯視角射擊,但他離一個完整的游戲還是太遠(yuǎn)了。還需要更加努力,了解更多同質(zhì)類型的游戲,也需要學(xué)習(xí)更多類型的游戲。
總結(jié)
以上是生活随笔為你收集整理的【Unity俯视角射击】我们来做一个《元气骑士》的完整Demo1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: string数据库使用和实践的第二部分网
- 下一篇: 数据库时间为datetime(date)