前提
unityで2Dアクションゲームを作っています。
実現したいこと
セーブポイントに触れたらリスタートするときにそこから出現する。
ここに実現したいことを箇条書きで書いてください。
発生している問題・エラーメッセージ
playerが触れても座標が登録されない。
エラーメッセージ
なし
セーブポイントオブジェクトのコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SavePoint : MonoBehaviour
{
public static Vector3 PlayerStartPoint;
void Start() { PlayerStartPoint = GameObject.Find("Player").transform.position; } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D collision) { if(collision.tag=="Player") { Debug.Log("s"); PlayerStartPoint = this.transform.position; } }
}
playerのソースコード using UnityEngine; using UnityEngine.UI; public class Playermanager : MonoBehaviour { [SerializeField] LayerMask blocklayer; [SerializeField] GameManager gameManager; [SerializeField] GameObject DeadAnimation; [SerializeField] AudioClip DeadSe; public AudioSource audiosource; public AnimationCurve DashCurve; public AnimationCurve JumpCurve; //スマホの移動用 public enum DIRECTION_TYPE { STOP, RIGHT, LEFT, } DIRECTION_TYPE direction = DIRECTION_TYPE.STOP; Rigidbody2D rigid; Animator animator; float speed; float jumpPower = 450; private void Start() { this.transform.position = SavePoint.PlayerStartPoint;//←取得したセーブポイントをよびだす。 rigid = GetComponent<Rigidbody2D>(); this.animator = GetComponent<Animator>(); audiosource = GetComponent<AudioSource>(); //float x = PlayerPrefs.GetFloat("PlayerxPos", 0); //float y = PlayerPrefs.GetFloat("PlayeryPos", 0); //transform.position = new Vector3(x, y, 0); } void Update() { float x = Input.GetAxis("Horizontal"); animator.SetFloat("Speed", Mathf.Abs(x)); if (x == 0) { direction = DIRECTION_TYPE.STOP; speed = 0; } else if (x < 0) { direction = DIRECTION_TYPE.RIGHT; speed = -5; transform.localScale = new Vector2(-1, 1); } else if (x > 0) { direction = DIRECTION_TYPE.LEFT; speed = 5; transform.localScale = new Vector2(1, 1); } if (IsGround()) { if (Input.GetKeyDown("space")) { Jump(); } } else { animator.SetBool("IsJump", false); } if (Input.GetKeyDown(KeyCode.R)) { dead(); } } private void FixedUpdate() { switch (direction) { case DIRECTION_TYPE.STOP: //speed = 0; break; case DIRECTION_TYPE.RIGHT: //speed = 3; break; case DIRECTION_TYPE.LEFT: //speed = -3; break; } rigid.position += new Vector2(speed * 0.02f, 0) ; // rigid.velocity = new Vector2(speed, rigid.velocity.y); } public void Jump() { rigid.AddForce(Vector2.up * jumpPower); animator.SetBool("IsJump", true); } bool IsGround() { Vector3 leftstartpooint = transform.position - Vector3.right * 0.2f; Vector3 rightstartpooint = transform.position + Vector3.right * 0.2f; Vector3 endpoint = transform.position - Vector3.up * 0.1f; Debug.DrawLine(leftstartpooint, endpoint); return Physics2D.Linecast(leftstartpooint, endpoint, blocklayer) || Physics2D.Linecast(rightstartpooint, endpoint, blocklayer); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Trap") { dead(); } else if (collision.tag == "Finish") { Debug.Log("Clear"); gameManager.GameClear(); } else if (collision.tag == "Item") { collision.gameObject.GetComponent<ItemManager>().GetItem(); } else if (collision.gameObject.tag == "Enemy") { EnemyManager enemy = collision.gameObject.GetComponent<EnemyManager>(); if (this.transform.position.y + 0.5f > enemy.transform.position.y) { rigid.velocity = new Vector2(rigid.velocity.x, 0); Jump(); //上から踏んだら enemy.DestroyEnemy(); } else { //下から踏んだら Destroy(this.gameObject); gameManager.GameOver(); } } } private void dead() { Debug.Log("Gameover"); this.gameObject.SetActive(false); audiosource.PlayOneShot(DeadSe); DeadAnimation.transform.position = new Vector3(this.transform.position.x, this.transform.position.y); DeadAnimation.SetActive(true); gameManager.GameOver(); }
試したこと
Debugで試したところ反応しました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。