ネットワーク上で、「動き」を同期する。自分のキャラだけを動かせるようにする。



自己のキャラだけ操作する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using Photon.Pun;
public class TestMove : MonoBehaviourPunCallbacks // ★変更
{
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
// 自分のキャラだけを操作できるようにする。
// ★条件の追加
if(photonView.IsMine)
{
moveDirection.z = Input.GetAxis("Vertical");
transform.Rotate(0, Input.GetAxis("Horizontal") * 6f, 0);
if (moveDirection.magnitude > 0.1f)
{
Vector3 globalDirection = transform.TransformDirection(moveDirection);
controller.Move(globalDirection * Time.deltaTime * 5);
}
moveDirection.y -= 9.8f * Time.deltaTime;
if (controller.isGrounded)
{
moveDirection.y = 0;
}
}
}
}

【2020版】BattleOnline(基礎/全34回)
他のコースを見る


自己のキャラだけ操作する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using Photon.Pun;
public class TestMove : MonoBehaviourPunCallbacks // ★変更
{
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
// 自分のキャラだけを操作できるようにする。
// ★条件の追加
if(photonView.IsMine)
{
moveDirection.z = Input.GetAxis("Vertical");
transform.Rotate(0, Input.GetAxis("Horizontal") * 6f, 0);
if (moveDirection.magnitude > 0.1f)
{
Vector3 globalDirection = transform.TransformDirection(moveDirection);
controller.Move(globalDirection * Time.deltaTime * 5);
}
moveDirection.y -= 9.8f * Time.deltaTime;
if (controller.isGrounded)
{
moveDirection.y = 0;
}
}
}
}

ネットワーク上で、「動き」を同期する。自分のキャラだけを動かせるようにする。