Unity/์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ)

ํฌํ†ค ๋ฌด๋ฃŒ๋ฒ„์ „ ์‚ฌ์šฉํ•˜๊ธฐ(3) ์ด๋™์‹œ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ณด์ด๊ธฐ ์œ„ํ•œ Lerp ์ถ”๊ฐ€

Rainbow๐ŸŒˆCoder 2022. 1. 4. 12:48
728x90

์•ž์„  ๊ธ€๊ณผ ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

Transform View ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋นผ๋„ ์ž˜ ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ + ์ฃผ์„

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
                                                //๊ด€์ธก ์ปจํŠธ๋กค๋Ÿฌ
                                                //์ƒํƒœ๋ณ€ํ™” ๊ด€์ธก(๋ฐ์ดํ„ฐ์˜ ์ฃผ๊ณ ๋ฐ›์Œ)
public class playerController : MonoBehaviourPunCallbacks, IPunObservable
{

    private Vector3 curPos;
    private Quaternion curRot;
    void Start()
    {

    }
    void Update()
    {
        if(photonView.IsMine)//๋‚ด๊ฐ€ ํด๋ผ์ด์–ธํŠธ์ผ๋•Œ
        {
            //ํ‰์†Œ์— ๊ตฌํ˜„ํ•˜๋˜ ํ”Œ๋ ˆ์ด์–ด ๋กœ์ง์„ ์—ฌ๊ธฐ์—๋‹ค๊ฐ€ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.
            float v = Input.GetAxis("Vertical");
            float h = Input.GetAxis("Horizontal");

            Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);
            transform.Translate(moveDir*Time.deltaTime*10f);

        }
        else//๋‚ด๊ฐ€ ๋ณด๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ํ”Œ๋ ˆ์ด์–ด
        {
            //OnPhotonSerializeView()์˜ else์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๋„ฃ์–ด์ค€๋‹ค
            transform.position = Vector3.Lerp(transform.position,curPos,Time.deltaTime*10f); //(1)๋„ฃ์–ด์คŒ
            transform.rotation = Quaternion.Lerp(transform.rotation,curRot,Time.deltaTime*10f); //(2) ๋„ฃ์–ด์คŒ
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if(stream.IsWriting)//์“ฐ๋Š” ์ƒํƒœ, ์ƒ๋Œ€๋ฐฉ์—๊ฒŒ ๋‚˜์˜ ์ •๋ณด๋ฅผ ๋ณด๋ƒ„(์• ๋‹ˆ๋ฉ”์ด์…˜, ์ƒ‰๊น” ๋ณ€ํ™”.. ๋“ฑ๋“ฑ)
        {//๋ณด๋‚ด๊ธฐ
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        }//๋ฐ›๊ธฐ
        else//๋ณด๋‚ธ๊ฑธ ๋˜‘๊ฐ™์ด ์ˆœ์„œ๋Œ€๋กœ ๋ฐ›์Œ
        {
            curPos = (Vector3)stream.ReceiveNext(); //(1)
            curRot = (Quaternion)stream.ReceiveNext(); //(2)
        }
    }

}
728x90