オリジナルキャラを操作する

ネットワーク上で個別にキャラを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加
using Photon.Pun;
public class PlayerController : MonoBehaviourPunCallbacks // 変更
{
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
// ★新しい記述方法をおさえよう!
TryGetComponent(out controller);
}
void Update()
{
// ★重要
// 自己に所有権があるオブジェクトだけ操作できるようにする。
if(photonView.IsMine)
{
if(controller.isGrounded)
{
var h = Input.GetAxis("Horizontal");
var v = Input.GetAxis("Vertical");
moveDirection = new Vector3(h, 0, v).normalized;
// ワールド空間へ変換(原点を基準とした座標系)
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * speed;
// ジャンプの実装
if(Input.GetKeyDown(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
}
// 疑似重力の実装
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
}
}
}








【2021版】BattleOnline(全37回)
他のコースを見る
ネットワーク上で個別にキャラを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加
using Photon.Pun;
public class PlayerController : MonoBehaviourPunCallbacks // 変更
{
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
// ★新しい記述方法をおさえよう!
TryGetComponent(out controller);
}
void Update()
{
// ★重要
// 自己に所有権があるオブジェクトだけ操作できるようにする。
if(photonView.IsMine)
{
if(controller.isGrounded)
{
var h = Input.GetAxis("Horizontal");
var v = Input.GetAxis("Vertical");
moveDirection = new Vector3(h, 0, v).normalized;
// ワールド空間へ変換(原点を基準とした座標系)
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * speed;
// ジャンプの実装
if(Input.GetKeyDown(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
}
// 疑似重力の実装
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
}
}
}








オリジナルキャラを操作する