page.phpテンプレートを利用しています。
//playerに
using UnityEngine;
public class MovingObj : MonoBehaviour
{
//プレイヤーのスピード
public float joystickMoveSpeed = 5f;
//インスペクターから『Fixed Joystick』を追加。
public Joystick joystick;
//ジョイスティックの傾き度を取得
Vector3 joystickMoveVector;
private Animator anim;
private Rigidbody2D rb;
void Start()
{
anim = GetComponent();
// rigid = GetComponent();
rb = GameObject.Find("Player").GetComponent();
}
void Update()
{
if (!Input.GetMouseButton(0))
{
anim.SetBool("del", false);
return;
}
joystickMoveVector = Vector3.right * joystick.Horizontal + Vector3.up * joystick.Vertical;
if (joystickMoveVector != Vector3.zero) //ジョイスティックを動かす
{
transform.Translate(joystickMoveVector * joystickMoveSpeed * Time.deltaTime, Space.World);
anim.SetBool("del", true);
GetComponent().flipX = true;
}
Vector3 lookDir = new Vector3(joystick.Direction.x, 0, 0);
transform.right = -lookDir;
}
}
///////////////////////////
//playerに
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public GameObject prefabBullet;
public GameObject prefabBullet2;
public Transform bulletPoint;
private bool ture;
public void Attack()
{
GetComponent().flipX = false;
Instantiate(prefabBullet2, bulletPoint.position, bulletPoint.rotation);
}
public void Attack2()
{
GetComponent().flipX = true;
Instantiate(prefabBullet, bulletPoint.position, bulletPoint.rotation);
}
}
///////////////////////////////
//bulletに
using UnityEngine;
public class Bullet : MonoBehaviour
{
void Update()
{
transform.position += new Vector3(-13f * Time.deltaTime, 0, 0);
}
}
_________________
public class Bullet2 : MonoBehaviour
{
void Update()
{
transform.position += new Vector3(13f * Time.deltaTime, 0, 0);
}
}
////////////////////
using UnityEngine;
public class Player : MonoBehaviour
{
public float joystickMoveSpeed = 5f;
public Joystick joystick;
Vector3 joystickMoveVector;
void Update()
{
if (!Input.GetMouseButton(0))
{
return;
}
joystickMoveVector = Vector3.zero;
joystickMoveVector.x = joystick.Horizontal;
joystickMoveVector.y = joystick.Vertical;
joystickMoveVector = Vector3.right * joystick.Horizontal + Vector3.up * joystick.Vertical * 1.7f;
if (joystickMoveVector != Vector3.zero)
{
transform.Translate(joystickMoveVector * joystickMoveSpeed * Time.deltaTime, Space.World);
}
else{
}
float angle = Mathf.Atan2(joystickMoveVector.y, joystickMoveVector.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}
//cameraにアタッチ
using UnityEngine;
public class CameraMove : MonoBehaviour
{
[SerializeField] Transform playerTr; // プレイヤーのTransform
[SerializeField] Vector3 cameraOrgPos = new Vector3(0, 0, -10f); // カメラの初期位置位置
Vector2 camaraMaxPos = new Vector2(22, 6); // カメラの右上限界座標
Vector2 camaraMinPos = new Vector2(-22,-6); // カメラの左下限界座標
void LateUpdate()
{
Vector3 playerPos = playerTr.position; // プレイヤーの位置
Vector3 camPos = transform.position; // カメラの位置
// 滑らかにプレイヤーの場所に追従
camPos = Vector3.Lerp(transform.position, playerPos + cameraOrgPos, 3.0f * Time.deltaTime);
// カメラの位置を制限
camPos.x = Mathf.Clamp(camPos.x, camaraMinPos.x, camaraMaxPos.x);
camPos.y = Mathf.Clamp(camPos.y, camaraMinPos.y, camaraMaxPos.y);
camPos.z = -10f;
transform.position = camPos;
}
}
///////////////////////////
using UnityEngine;
public class CameraScript : MonoBehaviour
{
public GameObject player;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 playerPos = player.transform.position;
//カメラとプレイヤーの位置を同じにする
transform.position = new Vector3(playerPos.x, playerPos.y, -10);
}
}
//SubCameraの設定
//SubCanvasの設定(Canvasをコピー)
// GameObjectにアタッチ
using UnityEngine;
using UnityEngine.UIElements;
public class Camera : MonoBehaviour
{
private GameObject mainCamera; //メインカメラ格納用
private GameObject subCamera; //サブカメラ格納用
[SerializeField] GameObject target; //Playerに設定
private Vector3 offset;
void Start()
{
mainCamera = GameObject.Find("MainCamera");
subCamera = GameObject.Find("SubCamera");
subCamera.SetActive(false);
}
void Update()
{
Vector3 playerPos = this.target.transform.position;
if (playerPos.x >= mainCamera.transform.position.x + 8f)
{
//サブカメラをアクティブに設定
mainCamera.SetActive(false);
subCamera.SetActive(true);
}
else if(playerPos.x >= mainCamera.transform.position.x - 10f)
{
//メインカメラをアクティブに設定
subCamera.SetActive(false);
mainCamera.SetActive(true);
}
}
}
//GameObjectに
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public GameObject objFinish;
public GameObject Player;
public Text txtScore;
public GameObject txtClear;
int iScore = 0;
int remaining;
[SerializeField] int timeLimit;
[SerializeField] Text timerText;
float time;
void Start()
{
txtScore.text = "Score:";
objFinish.SetActive(false);
txtClear.SetActive(false);
}
void Update()
{
time += Time.deltaTime;
//time変数をint型にし制限時間から引いた数をint型のlimit変数に代入
remaining = timeLimit - (int)time;
timerText.text = $"のこり:{remaining.ToString("D3")}";
if (remaining <= 0)
{
Player.gameObject.SetActive(false);
objFinish.SetActive(true);
Invoke("GameRestart", 3.5f);
}
}
public void SetScore()
{
iScore += 10;
txtScore.text = "Score:" + iScore.ToString();
if (iScore >= 300)
{
txtClear.SetActive(true);
Player.gameObject.SetActive(false);
Invoke("GameRestart", 3.5f);
}
}
public void GameOver()
{
objFinish.SetActive(true);
Invoke("GameRestart", 3.5f);
}
public void GameRestart()
{
Scene activeScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(activeScene.name);
}
}
//____________________________
//Ballに
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float Value = 0.1f;
public float kaiten;
public GameObject explosionPrefab;
Score sManager;
SePlay sePlay;
private float step_time;
bool canDieSoon;
void Start()
{
sManager = GameObject.Find("ScoreManager").GetComponent();
explosionPrefab = GameObject.FindWithTag("Particle");
sePlay = GameObject.Find("SePlayer").GetComponent();
step_time = 0.0f;
canDieSoon = false;
}
void LoadEnding()
{
SceneManager.LoadScene("MainScene");
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("bullet"))
{
Destroy(col.gameObject);//敵キャラを削除
Destroy(gameObject);//ミサイルを削除
sManager.SetScore();
sePlay.Play("SE2");
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
else if (col.gameObject.CompareTag("Player"))
{
col.gameObject.SetActive(false);
sePlay.Play("SE0");
sManager.GameOver();
}
}
void Update()
{
transform.position += new Vector3(-Value, 0, 0);
kaiten += 0.2f * Time.deltaTime;
transform.Rotate(0, 0, kaiten);
}
}