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




自分のキャラだけ動かせるようにする
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 * 5f);
}
moveDirection.y -= 3f * Time.deltaTime;
if (controller.isGrounded)
{
moveDirection.y = 0;
}
}
}
}

【2021版】BattleOnline(全37回)
他のコースを見る



自分のキャラだけ動かせるようにする
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 * 5f);
}
moveDirection.y -= 3f * Time.deltaTime;
if (controller.isGrounded)
{
moveDirection.y = 0;
}
}
}
}

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