KitsuController.csをどう書き換えてスマホバージョンにしたのか。備忘録として。
WebGL用に作った「きつねくん森へいく」ゲームのKitsuController.csをジョイスティックで動かせるように、アレンジした。元は変数moveとなっていたところを、全部
joystickMoveVector に入れ替えてみたところ、おおむね上手くいった。
赤文字👇の部分があたらしく挿入したジョイスティック用のコード。
緑文字の部分はコメントアウト。
きつねくんが左向きに歩くときに、アニメーションが右向きのままになってしまうのが、これから修正を必要とするところ。何が問題なのかを、探らなくては。(こん)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class KitsuController : MonoBehaviour
{
https://solitude-bocchi.com/unity/move/post-905で学んだジョイスティックのためのスクリプト
public Joystick joystick; //インスペクターにプレファブの『Fixed Joystick』を指定。
Vector3 joystickMoveVector; //ジョイスティックの傾き度を取得
public GameObject KitsuFishingPrefab;
public float speed = 3.0f;//これは不要か?
public int maxHealth = 5;
public GameObject projectilePrefab;
public int health { get { return currentHealth; } }
int currentHealth;
public float timeInvincible = 2.0f;
bool isInvincible;
float invincibleTimer;
Rigidbody2D rigidbody2d;
// float horizontal;
//float vertical;
Animator animator;
Vector2 lookDirection = new Vector2(1, 0);
AudioSource audioSource;
public void PlaySound(AudioClip clip)//木人や光の胞子に当たった時のキュン音
{
audioSource.PlayOneShot(clip);
}
// Start is called before the first frame update
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
currentHealth = maxHealth;
// Debug.Log(currentHealth);
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
currentHealth = maxHealth;
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
joystickMoveVector = Vector3.right * joystick.Horizontal + Vector3.up * joystick.Vertical;
if (joystickMoveVector != Vector3.zero) //ジョイスティックを動かすと動く。
{
transform.Translate(joystickMoveVector * joystickMoveSpeed * Time.deltaTime, Space.World); //ジョイスティックの傾き度で動くスピードが変わる。
}
}
//horizontal = Input.GetAxis("Horizontal");
// vertical = Input.GetAxis("Vertical");
// Vector2 move = new Vector2(horizontal, vertical);
//以降、moveの代わりに joystickMoveVectorを代入した。
if (!Mathf.Approximately(joystickMoveVector.x, 0.0f) || !Mathf.Approximately(joystickMoveVector.y, 0.0f))
{
lookDirection.Set(joystickMoveVector.x, joystickMoveVector.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", joystickMoveVector.magnitude);
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}
if (Input.GetKeyDown(KeyCode.C))//ここはスマホゲームではCキーでなくシューティングボタンをクリックに変える
{
Launch();//小瓶を投げるという関数
}
/* if (Input.GetKeyDown(KeyCode.X))
{
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
if (hit.collider != null)
{
NonPlayerCharacter character = hit.collider.GetComponent<NonPlayerCharacter>();
if (character != null)
{
character.DisplayDialog();
}
}
}*/
}
/* void FixedUpdate()//キー操作によるキツの移動
{
//Vector2 position = rigidbody2d.position;
//position.x = position.x + speed * horizontal * Time.deltaTime;
//position.y = position.y + speed * vertical * Time.deltaTime;
//rigidbody2d.MovePosition(position);
// Debug.Log("現在の体力は" + currentHealth);
}
*/
public void ChangeHealth(int amount)//ゾンビに当たるとライフが減る
{
if (amount < 0)
{
if (isInvincible)
return;
isInvincible = true;
invincibleTimer = timeInvincible;
}
currentHealth = Mathf.Clamp(currentHealth + amount, -1, maxHealth);
// Debug.Log("現在の体力は" + currentHealth);
if (currentHealth == 0)
{
//Debug.Log("体力ゼロ");
GotoGameOverScene();//体力がゼロになったらゲームオーバーシーンへ。
}
UIHealthBar.instance.SetValue(currentHealth / (float)maxHealth);
}
public void GotoGameOverScene()
{
SceneManager.LoadScene("GameOverScene");
}
public void Launch()//他のスクリプト、例えばShootingButtonからこの関数を取得するためにpublicを付けたほうがよいのかなと、思ってpublicにしてみた。
{
GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + (Vector2.up * 0.5f), Quaternion.identity);
Projectile projectile = projectileObject.GetComponent<Projectile>();
projectile.Launch(lookDirection, 300);
animator.SetTrigger("Launch");//アニメーションを投げる動作にする
}
//FishingEreaに入ったらkitsuを消して、fishingKitsuを生成するという関数
void OnTriggerEnter2D(Collider2D other)
{
//Fishing = GameObject.FindGameObjectWithTag("Fishing");
if (other.CompareTag("Fishing"))
{
Destroy(gameObject);
Instantiate(KitsuFishingPrefab, transform.position, transform.rotation);
}
}