スパイダービームの作成
スパイダーモードの実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
private float speed;
private Vector3 movement;
private CharacterController controller;
private int clickCount = 0;
public float runSpeed;
private Vector3 moveDir = Vector3.zero;
private float gravity = 3f;
public float jumpPower;
public AudioClip jumpSound;
private AudioSource audioS;
public AudioClip footSound;
private float runStep = 0.7f;
private float stepInterval = 10f;
private float stepCycle;
private float nextStep;
private bool isWalking = true;
// ★追加(スパイダー)
public GameObject spiderTarget;
// ★追加(スパイダー)
private enum State
{
Normal,
Spider
}
// ★追加(スパイダー)
private State playerState;
void Start()
{
// ★追加(スパイダー)
playerState = State.Normal;
spiderTarget.transform.position = Vector3.zero;
audioS = GetComponent<AudioSource>();
controller = GetComponent<CharacterController>();
speed = walkSpeed;
}
void Update()
{
// ★追加&改良(スパイダー)
switch (playerState)
{
case State.Normal:
PlayerMove(); // PlayerMove()をここに移動させる。
break;
case State.Spider:
SpiderMove();
break;
}
// ★追加(スパイダー)
// ここではQボタンを押すとノーマルステートに戻るようにします。
if(Input.GetKeyDown(KeyCode.Q))
{
SpiderEnd();
}
}
// Stateが「Normal」の時はこちらを実行する(ポイント)
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = Camera.main.transform.forward * movement.z + Camera.main.transform.right * movement.x;
moveDir.x = desiredMove.x * 3f;
moveDir.z = desiredMove.z * 3f;
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime * speed);
if (Input.GetKeyDown(KeyCode.W))
{
clickCount += 1;
Invoke("ResetCount", 0.3f);
}
if (Input.GetKeyUp(KeyCode.W))
{
speed = walkSpeed;
isWalking = true;
}
if (controller.isGrounded)
{
moveDir.y = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioS.clip = jumpSound;
audioS.Play();
}
}
StepSound();
}
void ResetCount()
{
if (clickCount != 2)
{
clickCount = 0;
return;
}
else
{
clickCount = 0;
speed = runSpeed;
isWalking = false;
}
}
void StepSound()
{
if (!controller.isGrounded)
{
return;
}
if (controller.velocity.sqrMagnitude > 0 && (movement.x != 0 || movement.z != 0))
{
stepCycle += (controller.velocity.magnitude + (speed * (isWalking ? 1f : runStep))) * Time.deltaTime;
}
if (stepCycle < nextStep)
{
return;
}
nextStep = stepCycle + stepInterval;
audioS.clip = footSound;
audioS.PlayOneShot(audioS.clip);
}
// ★追加(スパイダー)
// Stateが「Spider」の時はこちらを実行する(ポイント)
void SpiderMove()
{
Vector3 moveDir = (spiderTarget.transform.position - transform.position).normalized;
float flyingSpeed = Vector3.Distance(transform.position, spiderTarget.transform.position) * 2f; // 2点間の距離が大きいほど速度も大きくなる。
controller.Move(moveDir * flyingSpeed * Time.deltaTime);
}
// ★追加(スパイダー)
// 外部のスクリプトからステータスの変更ができるようにする(ポイント)
public void SpiderStart()
{
playerState = State.Spider;
}
public void SpiderEnd()
{
playerState = State.Normal;
}
}
スパイダービーム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpiderBeam : BeamBase // 追加
{
private GameObject spiderTarget;
private GameObject player;
void Start()
{
spiderTarget = GameObject.Find("SpiderTarget");
player = GameObject.Find("Player");
}
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
// ビーム着弾点にスパイダーターゲットを設置する。
spiderTarget.transform.position = this.transform.position;
AudioSource.PlayClipAtPoint(Sound, Camera.main.transform.position);
// PlayerのStateを「Spider」に変更する(ポイント)
player.GetComponent<PlayerController>().SpiderStart();
}
}
【2021版】X_Mission(全34回)
他のコースを見るスパイダーモードの実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
private float speed;
private Vector3 movement;
private CharacterController controller;
private int clickCount = 0;
public float runSpeed;
private Vector3 moveDir = Vector3.zero;
private float gravity = 3f;
public float jumpPower;
public AudioClip jumpSound;
private AudioSource audioS;
public AudioClip footSound;
private float runStep = 0.7f;
private float stepInterval = 10f;
private float stepCycle;
private float nextStep;
private bool isWalking = true;
// ★追加(スパイダー)
public GameObject spiderTarget;
// ★追加(スパイダー)
private enum State
{
Normal,
Spider
}
// ★追加(スパイダー)
private State playerState;
void Start()
{
// ★追加(スパイダー)
playerState = State.Normal;
spiderTarget.transform.position = Vector3.zero;
audioS = GetComponent<AudioSource>();
controller = GetComponent<CharacterController>();
speed = walkSpeed;
}
void Update()
{
// ★追加&改良(スパイダー)
switch (playerState)
{
case State.Normal:
PlayerMove(); // PlayerMove()をここに移動させる。
break;
case State.Spider:
SpiderMove();
break;
}
// ★追加(スパイダー)
// ここではQボタンを押すとノーマルステートに戻るようにします。
if(Input.GetKeyDown(KeyCode.Q))
{
SpiderEnd();
}
}
// Stateが「Normal」の時はこちらを実行する(ポイント)
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = Camera.main.transform.forward * movement.z + Camera.main.transform.right * movement.x;
moveDir.x = desiredMove.x * 3f;
moveDir.z = desiredMove.z * 3f;
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime * speed);
if (Input.GetKeyDown(KeyCode.W))
{
clickCount += 1;
Invoke("ResetCount", 0.3f);
}
if (Input.GetKeyUp(KeyCode.W))
{
speed = walkSpeed;
isWalking = true;
}
if (controller.isGrounded)
{
moveDir.y = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioS.clip = jumpSound;
audioS.Play();
}
}
StepSound();
}
void ResetCount()
{
if (clickCount != 2)
{
clickCount = 0;
return;
}
else
{
clickCount = 0;
speed = runSpeed;
isWalking = false;
}
}
void StepSound()
{
if (!controller.isGrounded)
{
return;
}
if (controller.velocity.sqrMagnitude > 0 && (movement.x != 0 || movement.z != 0))
{
stepCycle += (controller.velocity.magnitude + (speed * (isWalking ? 1f : runStep))) * Time.deltaTime;
}
if (stepCycle < nextStep)
{
return;
}
nextStep = stepCycle + stepInterval;
audioS.clip = footSound;
audioS.PlayOneShot(audioS.clip);
}
// ★追加(スパイダー)
// Stateが「Spider」の時はこちらを実行する(ポイント)
void SpiderMove()
{
Vector3 moveDir = (spiderTarget.transform.position - transform.position).normalized;
float flyingSpeed = Vector3.Distance(transform.position, spiderTarget.transform.position) * 2f; // 2点間の距離が大きいほど速度も大きくなる。
controller.Move(moveDir * flyingSpeed * Time.deltaTime);
}
// ★追加(スパイダー)
// 外部のスクリプトからステータスの変更ができるようにする(ポイント)
public void SpiderStart()
{
playerState = State.Spider;
}
public void SpiderEnd()
{
playerState = State.Normal;
}
}
スパイダービーム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpiderBeam : BeamBase // 追加
{
private GameObject spiderTarget;
private GameObject player;
void Start()
{
spiderTarget = GameObject.Find("SpiderTarget");
player = GameObject.Find("Player");
}
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
// ビーム着弾点にスパイダーターゲットを設置する。
spiderTarget.transform.position = this.transform.position;
AudioSource.PlayClipAtPoint(Sound, Camera.main.transform.position);
// PlayerのStateを「Spider」に変更する(ポイント)
player.GetComponent<PlayerController>().SpiderStart();
}
}
スパイダービームの作成