1. 程式人生 > >Unity NetWorking 不用NetWorkTransform

Unity NetWorking 不用NetWorkTransform


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Move : NetworkBehaviour
{

    [SyncVar]
    private Vector3 v3_PlayerPos;

    [SyncVar]
    private Quaternion qua_PlayerRotate;

    private float my_Speed = 10f;
    private
float my_RotateSpeed = 60f; void Update() { if (!isLocalPlayer) { return; } float ver = Input.GetAxis("Vertical"); float Hor = Input.GetAxis("Horizontal"); transform.Translate(Hor * Time.deltaTime * my_Speed, 0, ver * my_Speed * Time.deltaTime); transform.Rotate(new
Vector3(0, my_RotateSpeed * Hor * Time.deltaTime, 0)); } void FixedUpdate() { //如果是本地建立 則把資料更新至伺服器 通過SyncVar 傳送給所有的客戶端 if (isLocalPlayer) { CmdSendServerPos(transform.position,transform.rotation); } else { //如果不是本地建立 則 插值移動(所謂的映象移動)
LerpPosition(); } } //插值移動 void LerpPosition() { transform.position = Vector3.Lerp(transform.position, v3_PlayerPos, 5 * Time.fixedDeltaTime); transform.rotation = Quaternion.Lerp(transform.rotation,qua_PlayerRotate,5 * Time.fixedDeltaTime); } [Command] public void CmdSendServerPos(Vector3 pos,Quaternion rotate) { v3_PlayerPos = pos; qua_PlayerRotate = rotate; } }