You are currently viewing 【Unity教學】2022製作多人連線遊戲的最佳選擇!Photon Fusion 04 跳躍及發射子彈

【Unity教學】2022製作多人連線遊戲的最佳選擇!Photon Fusion 04 跳躍及發射子彈

  • Post category:教學
  • Reading time:4 mins read
  • Post author:

大家好,我是Wilson,這次的主題是讓玩家跳躍以及發射子彈!

領取Photon Fusion 快速上手懶人攻略

這邊幫大家準備了【Photon Fusion 快速上手懶人攻略】!

裡面介紹了Fusion的基本功能,以及Fusion同步網路狀態的3大方法,有興趣的朋友可以免費領取喔!

重點回顧

  • 可以使用NetworkButtons紀錄要傳送的自定義按鈕輸入,並用enum來定義需要使用哪些按鈕
  • NetworkButtons.Set()方法來設定按鈕的狀態
  • 如果要判斷按鈕是否是被「剛按下去的狀態」的話,我們會需要將現在的NetworkButton與上一個Tick的NetworkButton做比較,可以透過NetworkButton.GetPressed()方法傳入上一個Tick的NetworkButton資料來判斷
  • 透過NetworkButton.IsSet()方法來判斷按鈕的狀態
  • 使用Network Transform元件可以同步網路物件的Transform
  • Fusion提供了TickTimer幫助我們可以做到以Tick為基準的計時器,讓計時更準確
  • Spawned()方法就是當網路物件剛生成時會呼叫一次,就類似MonoBehaviour的Start()方法
  • 我們可以透過NetworkBehaviour的Runner變數找到場景中的NetworkRunner
  • NetworkBehaviour的Object變數則代表該NetworkBehaviour身上的Network Object元件
  • Network Rigidbody就是Rigidbody的網路版本,使用這個就可以實現網路上的物理模擬囉
  • 我們可以在Network Project Config中的Server Physics Mode改成Client Prediction,就可以實現客戶端的物理預測,讓物件移動更加平滑

Code

NetworkInputData.cs

public enum InputButtons
{
    JUMP,
    FIRE
}

public struct NetworkInputData : INetworkInput
{
    public NetworkButtons buttons;
    public Vector3 movementInput;
}

OnInput()提供NetworkButton的輸入

data.buttons.Set(InputButtons.JUMP, Input.GetKey(KeyCode.Space));
data.buttons.Set(InputButtons.FIRE, Input.GetKey(KeyCode.Mouse0));

在PlayerController.cs接收NetworkButton的輸入

if (GetInput(out NetworkInputData data))
{
    NetworkButtons buttons = data.buttons;
    var pressed = buttons.GetPressed(ButtonsPrevious);
    ButtonsPrevious = buttons;

    /*與上次相同的部分略*/

    if (pressed.IsSet(InputButtons.JUMP))
    {
        networkCharacterController.Jump();
    }

    if (pressed.IsSet(InputButtons.FIRE))
    {
        Runner.Spawn(
            bulletPrefab, 
            transform.position + transform.TransformDirection(Vector3.forward),
            Quaternion.LookRotation(transform.TransformDirection(Vector3.forward)),
            Object.InputAuthority);
    }
}

Bullet.cs

public class Bullet : NetworkBehaviour
{
    [Networked]
    private TickTimer life { get; set; }

    [SerializeField]
    private float bulletSpeed = 5f;

    public override void Spawned()
    {
        life = TickTimer.CreateFromSeconds(Runner, 5.0f);
    }

    public override void FixedUpdateNetwork()
    {
        if (life.Expired(Runner))
        {
            Runner.Despawn(Object);
        }
        else
        {
            transform.position += bulletSpeed * transform.forward * Runner.DeltaTime;
        }

    }
}

專案GitHub

這次進度的專案可以在這裡看到!

This Post Has 9 Comments

  1. Corey

    Awesome content, can’t wait to learn more about Photon Fusion, hopefully, more to come from here!

  2. 831

    constantly i sed to read smalller posts which also clear thdir motive, annd that
    is aalso happening with this piece of writting which I aam reading now.

  3. 394

    I was recommended this blog byy my cousin. I
    amm not sure wwhether this post iss written by him as
    no one elose kow such detailed abou myy problem. You’re incredible!

    Thanks!

  4. 618

    I’ve beeen exploring for a liittle foor any high-quality articles or blog posts in this sort of houuse .
    Explooring in Yahoo I ultimately sumbled uoon this website.
    Reading this invo So i aam satisfierd to exlress thaqt I’ve an incrediibly jus
    right unncanny feeling I discovered exactly what I
    needed. I most no doubt will make sure to don?t foorget this site and give
    it a glance on a continuiing basis.

  5. Kaley

    I don’t even know hoow I stopped up right here, butt I thought thhis pput
    up use to be great. I don’t recognise whho yyou migh be bbut definitely
    you’re going too a well-known blogger whnen you are
    nnot already. Cheers!

  6. 920

    Thiss excellent website truly has all of thee inflrmation I needed
    concerning this subject aand didn’t kow who to ask.

  7. Trista

    Thanmks forr thee auspicious writeup. It actually wwas a enhtertainment
    accpunt it. Glance complicated too far added agreeable from you!
    By thhe way, how ckuld we keep iin touch?

  8. It iis appropriate time too make some pllans forr the future and it
    is time tto bee happy. I’ve rad this ost andd iif Icold I desire
    tto sugggest youu feww interesting thingss or suggestions.
    Perhaps you can write nerxt articles referring too this article.
    I widh tto read even more things about it!

Leave a Reply