ジャンプ台(グラスホッパー)の作成
ジャンプ台の生成
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;
private Animator animator;
public GameObject genePoint;
void Start()
{
TryGetComponent(out controller);
animator = GetComponent<Animator>();
}
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;
animator.SetFloat("Front", moveDirection.z, 0.1f, Time.deltaTime);
animator.SetFloat("Side", moveDirection.x, 0.1f, Time.deltaTime);
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);
if(!controller.isGrounded)
{
if(Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(ScaffoldManagement());
}
}
// ★追加
// GlassHopper(ジャンプ台)の生成
if(!controller.isGrounded)
{
if(Input.GetKeyDown(KeyCode.Q))
{
StartCoroutine(GlassHopperManagement());
}
}
}
}
private IEnumerator ScaffoldManagement()
{
GameObject scaffold = PhotonNetwork.Instantiate("Scaffold", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
PhotonNetwork.Destroy(scaffold);
}
// ★追加
// GlassHopper
private IEnumerator GlassHopperManagement()
{
GameObject glassHopper = PhotonNetwork.Instantiate("GlassHopper", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
PhotonNetwork.Destroy(glassHopper);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("GlassHopper"))
{
moveDirection.y = 25f;
}
}
}
【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;
private Animator animator;
public GameObject genePoint;
void Start()
{
TryGetComponent(out controller);
animator = GetComponent<Animator>();
}
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;
animator.SetFloat("Front", moveDirection.z, 0.1f, Time.deltaTime);
animator.SetFloat("Side", moveDirection.x, 0.1f, Time.deltaTime);
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);
if(!controller.isGrounded)
{
if(Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(ScaffoldManagement());
}
}
// ★追加
// GlassHopper(ジャンプ台)の生成
if(!controller.isGrounded)
{
if(Input.GetKeyDown(KeyCode.Q))
{
StartCoroutine(GlassHopperManagement());
}
}
}
}
private IEnumerator ScaffoldManagement()
{
GameObject scaffold = PhotonNetwork.Instantiate("Scaffold", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
PhotonNetwork.Destroy(scaffold);
}
// ★追加
// GlassHopper
private IEnumerator GlassHopperManagement()
{
GameObject glassHopper = PhotonNetwork.Instantiate("GlassHopper", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
PhotonNetwork.Destroy(glassHopper);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("GlassHopper"))
{
moveDirection.y = 25f;
}
}
}
ジャンプ台(グラスホッパー)の作成