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

オリジナルキャラを操作する
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); } } }
C#







【2020版】BattleOnline(基礎/全34回)
他のコースを見る
オリジナルキャラを操作する
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); } } }
C#







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