複数の武器を切り替える


WeaponController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponController : MonoBehaviour {
// (復習)
// 「配列」について復習しましょう!
public GameObject[] weapons;
public GameObject[] shotWeapons;
public AudioClip changeSound;
public int currentNum = 0;
public GameObject aimObject;
void Start(){
// 「for文」の「意味」と「使い方」を復習しましょう。
for (int i = 0; i < weapons.Length; i++) {
if (i == currentNum) {
weapons [i].SetActive (true);
} else {
weapons [i].SetActive (false);
}
}
for (int i = 0; i < shotWeapons.Length; i++) {
if (i == currentNum) {
shotWeapons [i].SetActive (true);
} else {
shotWeapons [i].SetActive (false);
}
}
}
void Update(){
// もしも「右クリック」を押した場合には、
if (Input.GetMouseButtonDown (1)) {
AudioSource.PlayClipAtPoint (changeSound, Camera.main.transform.position);
// (重要テクニック)
// 配列の中の順序を1つずつ繰り上げていくテクニック
// 実際に「currentNum」の箱に中に入る数字がどう変化するか書き出してみましょう。
currentNum = (currentNum + 1) % weapons.Length;
for (int i = 0; i < weapons.Length; i++) {
if (i == currentNum) {
weapons [i].SetActive (true);
} else {
weapons [i].SetActive (false);
}
}
for (int i = 0; i < shotWeapons.Length; i++) {
if (i == currentNum) {
shotWeapons [i].SetActive (true);
} else {
shotWeapons [i].SetActive (false);
}
}
}
// もしも「左クリック」を押した場合には、
if (Input.GetMouseButton (0)) {
// 照準器を表示する。
aimObject.SetActive(true);
// 現在選択しているメインカメラモードの武器が非表示(オフ)になる。
weapons [currentNum].SetActive (false);
// 現在選択しているサブカメラモードの武器が表示(オン)になる。
shotWeapons[currentNum].SetActive(true);
} else {
// 照準器を非表示にする。
aimObject.SetActive(false);
// 現在選択しているメインカメラモードの武器が表示(オン)になる。
weapons [currentNum].SetActive (true);
// 現在選択しているサブカメラモードの武器が非表示(オフ)になる。
shotWeapons[currentNum].SetActive(false);
}
}
}








CameraMoveController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMoveController : MonoBehaviour {
public GameObject player;
public float rotateSpeed;
private const float angleLimitUp = 30f;
private const float angleLimitDown = -60f;
public Camera mainCamera;
public Camera subCamera;
private Vector3 angle;
// ★以下の武器及び照準器に関するコードを削除してください。
//public GameObject aimObject;
//public GameObject rifle;
//public GameObject shotBullet2;
//public GameObject shotRocket2;
//public GameObject rocketLauncher;
void Start(){
mainCamera.enabled = true;
subCamera.enabled = false;
}
void Update () {
transform.position = player.transform.position;
angle = player.transform.eulerAngles;
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, angle.y, transform.eulerAngles.z);
if (Input.GetMouseButton (0)) {
RotateCameraAngle ();
mainCamera.enabled = false;
subCamera.enabled = true;
// ★以下の武器及び照準器に関するコードを削除してください。
//aimObject.SetActive(true);
//rifle.SetActive (false);
//shotBullet2.SetActive(true);
//shotRocket2.SetActive(true);
//rocketLauncher.SetActive (false);
} else {
mainCamera.enabled = true;
subCamera.enabled = false;
// ★以下の武器及び照準器に関するコードを削除してください。
//aimObject.SetActive(false);
//rifle.SetActive (true);
//shotBullet2.SetActive(false);
//shotRocket2.SetActive(false);
//rocketLauncher.SetActive (true);
}
float angle_x = 180f <= transform.eulerAngles.x ? transform.eulerAngles.x - 360f : transform.eulerAngles.x;
transform.eulerAngles = new Vector3(Mathf.Clamp(angle_x, angleLimitDown, angleLimitUp), transform.eulerAngles.y, transform.eulerAngles.z);
}
void RotateCameraAngle(){
Vector3 angle = new Vector3(Input.GetAxis("Mouse X") * rotateSpeed, -Input.GetAxis("Mouse Y") *rotateSpeed, 0);
transform.eulerAngles += new Vector3(angle.y, angle.x, 0);
}
}


EscapeCombat(メモ)
他のコースを見る

WeaponController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponController : MonoBehaviour {
// (復習)
// 「配列」について復習しましょう!
public GameObject[] weapons;
public GameObject[] shotWeapons;
public AudioClip changeSound;
public int currentNum = 0;
public GameObject aimObject;
void Start(){
// 「for文」の「意味」と「使い方」を復習しましょう。
for (int i = 0; i < weapons.Length; i++) {
if (i == currentNum) {
weapons [i].SetActive (true);
} else {
weapons [i].SetActive (false);
}
}
for (int i = 0; i < shotWeapons.Length; i++) {
if (i == currentNum) {
shotWeapons [i].SetActive (true);
} else {
shotWeapons [i].SetActive (false);
}
}
}
void Update(){
// もしも「右クリック」を押した場合には、
if (Input.GetMouseButtonDown (1)) {
AudioSource.PlayClipAtPoint (changeSound, Camera.main.transform.position);
// (重要テクニック)
// 配列の中の順序を1つずつ繰り上げていくテクニック
// 実際に「currentNum」の箱に中に入る数字がどう変化するか書き出してみましょう。
currentNum = (currentNum + 1) % weapons.Length;
for (int i = 0; i < weapons.Length; i++) {
if (i == currentNum) {
weapons [i].SetActive (true);
} else {
weapons [i].SetActive (false);
}
}
for (int i = 0; i < shotWeapons.Length; i++) {
if (i == currentNum) {
shotWeapons [i].SetActive (true);
} else {
shotWeapons [i].SetActive (false);
}
}
}
// もしも「左クリック」を押した場合には、
if (Input.GetMouseButton (0)) {
// 照準器を表示する。
aimObject.SetActive(true);
// 現在選択しているメインカメラモードの武器が非表示(オフ)になる。
weapons [currentNum].SetActive (false);
// 現在選択しているサブカメラモードの武器が表示(オン)になる。
shotWeapons[currentNum].SetActive(true);
} else {
// 照準器を非表示にする。
aimObject.SetActive(false);
// 現在選択しているメインカメラモードの武器が表示(オン)になる。
weapons [currentNum].SetActive (true);
// 現在選択しているサブカメラモードの武器が非表示(オフ)になる。
shotWeapons[currentNum].SetActive(false);
}
}
}








CameraMoveController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMoveController : MonoBehaviour {
public GameObject player;
public float rotateSpeed;
private const float angleLimitUp = 30f;
private const float angleLimitDown = -60f;
public Camera mainCamera;
public Camera subCamera;
private Vector3 angle;
// ★以下の武器及び照準器に関するコードを削除してください。
//public GameObject aimObject;
//public GameObject rifle;
//public GameObject shotBullet2;
//public GameObject shotRocket2;
//public GameObject rocketLauncher;
void Start(){
mainCamera.enabled = true;
subCamera.enabled = false;
}
void Update () {
transform.position = player.transform.position;
angle = player.transform.eulerAngles;
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, angle.y, transform.eulerAngles.z);
if (Input.GetMouseButton (0)) {
RotateCameraAngle ();
mainCamera.enabled = false;
subCamera.enabled = true;
// ★以下の武器及び照準器に関するコードを削除してください。
//aimObject.SetActive(true);
//rifle.SetActive (false);
//shotBullet2.SetActive(true);
//shotRocket2.SetActive(true);
//rocketLauncher.SetActive (false);
} else {
mainCamera.enabled = true;
subCamera.enabled = false;
// ★以下の武器及び照準器に関するコードを削除してください。
//aimObject.SetActive(false);
//rifle.SetActive (true);
//shotBullet2.SetActive(false);
//shotRocket2.SetActive(false);
//rocketLauncher.SetActive (true);
}
float angle_x = 180f <= transform.eulerAngles.x ? transform.eulerAngles.x - 360f : transform.eulerAngles.x;
transform.eulerAngles = new Vector3(Mathf.Clamp(angle_x, angleLimitDown, angleLimitUp), transform.eulerAngles.y, transform.eulerAngles.z);
}
void RotateCameraAngle(){
Vector3 angle = new Vector3(Input.GetAxis("Mouse X") * rotateSpeed, -Input.GetAxis("Mouse Y") *rotateSpeed, 0);
transform.eulerAngles += new Vector3(angle.y, angle.x, 0);
}
}


複数の武器を切り替える