(2025年9月6日)
<プレーヤーの動きの修正>
・Human Cameraスクリプトは外す
・Exitへのトランジションの作成
・下記コードの修正
(2025年8月23日)
<プレーヤーの移動>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using System.Reflection.Emit;
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;
private GameObject glassHopper;
// ★追加(武器)
public GameObject weapon;
public AudioClip shotSound;
public GameObject shotPoint;
// ★★
private Camera cam;
// ★角度
private Quaternion currentRot;
void Start()
{
TryGetComponent(out controller);
animator = GetComponent<Animator>();
// ★追加(武器)
weapon.SetActive(false);
// ★★
cam = Camera.main;
}
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;
// ★★
Vector3 forward = cam.transform.forward;
forward.y = 0;
Vector3 right = cam.transform.right;
//print("forward " + forward);
//print("right " + right);
moveDirection = forward * v + right * h;
// ★追加(進行方向に合わせる)
if (moveDirection.magnitude >= 0.1f)
{
transform.rotation = Quaternion.LookRotation(moveDirection);
}
// ★改良(アニメーション)
animator.SetFloat("MovePower", new Vector3(h, 0, v).magnitude);
moveDirection = moveDirection * speed;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
// ★追加(ショット)
if (h == 0 && v == 0)
{
if (Input.GetKeyDown(KeyCode.P))
{
StartCoroutine(ShotManager());
StartCoroutine(BulletManager());
// ★RPC
photonView.RPC("ShotRPC", RpcTarget.Others);
}
}
}
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
if (!controller.isGrounded)
{
if (Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(ScafoldManager());
}
if (Input.GetKeyDown(KeyCode.Z))
{
StartCoroutine(GlassHopperManager());
}
}
}
}
private IEnumerator ScafoldManager()
{
GameObject scafold = PhotonNetwork.Instantiate("Scafold", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(10f);
PhotonNetwork.Destroy(scafold);
}
private IEnumerator GlassHopperManager()
{
glassHopper = PhotonNetwork.Instantiate("GlassHopper", genePoint.transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
if (glassHopper)
{
PhotonNetwork.Destroy(glassHopper);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("GlassHopper"))
{
moveDirection.y = 25f;
PhotonNetwork.Destroy(glassHopper);
}
}
// ★追加(ショット)
private IEnumerator ShotManager()
{
weapon.SetActive(true);
animator.SetTrigger("Shot");
yield return new WaitForSeconds(0.5f);
weapon.SetActive(false);
}
// ★追加(ショット)
private IEnumerator BulletManager()
{
GameObject bullet = PhotonNetwork.Instantiate("Bullet", shotPoint.transform.position, transform.rotation);
Rigidbody bulletRb = bullet.GetComponent<Rigidbody>();
bulletRb.AddForce(transform.forward * 1000);
AudioSource.PlayClipAtPoint(shotSound, Camera.main.transform.position);
yield return new WaitForSeconds(3f);
if(bullet)
{
PhotonNetwork.Destroy(bullet.gameObject);
}
}
// ★追加(ショット)
[PunRPC]
void ShotRPC()
{
StartCoroutine(ShotManager());
}
}
(2025年1月18日)
<無敵状態時に、色を変化させる>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
public TextMeshProUGUI hpLabel;
private int tankMaxHP = 10;
private bool isMuteki = false;
// ★「UnityEngine.」を追加
public UnityEngine.Material[] mats;
public GameObject[] parts;
private void Start()
{
tankHP = tankMaxHP;
hpLabel.text = "" + tankHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("EnemyShell") && isMuteki == false)
{
tankHP -= 1;
hpLabel.text = "" + tankHP;
Destroy(other.gameObject);
if (tankHP > 0)
{
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
Destroy(effect1, 1.0f);
}
else
{
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
//Destroy(gameObject);
this.gameObject.SetActive(false);
Invoke("GoToGameOver", 1.5f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
public void AddHP(int amount)
{
tankHP += amount;
if (tankHP > tankMaxHP)
{
tankHP = tankMaxHP;
}
hpLabel.text = "" + tankHP;
}
public void ReduceHP(int amount)
{
tankHP -= amount;
hpLabel.text = "" + tankHP;
if (tankHP < 0)
{
SceneManager.LoadScene("GameOver");
}
}
public void Muteki()
{
tankHP -= 0;
isMuteki = true;
Invoke("Super", 3);
// ★追加
// 色を黄金に変化
foreach(GameObject p in parts)
{
p.GetComponent<MeshRenderer>().material = mats[1];
}
}
void Super()
{
isMuteki = false;
// ★追加
// 色を初期に戻す
foreach(GameObject p in parts)
{
p.GetComponent<MeshRenderer>().material = mats[0];
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Doragon")&& isMuteki == false)
{
tankHP -= 1;
hpLabel.text = "" + tankHP;
if (tankHP < 0)
{
SceneManager.LoadScene("GameOver");
}
}
}
}
//次回の課題
//今、無敵状態だと分かるようにする。
//↑タンクのマテリアルが変わるなど。