質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1817閲覧

セーブポイントに触れたら その座標を取得してリスタート

Helpme

総合スコア1

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/04/21 07:52

前提

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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

SavePoint が読めない代物だし余計なコードが多すぎますが、SavePoint の方にリスポーン座標を保存している時点でそれが問題だということはわかりました。リスポーン座標は「プレイヤーを動かすクラス」が持っているべきです。

C#

1using UnityEngine; 2 3[RequireComponent(typeof(Rigidbody2D))] 4public class PlayerSimpleMove2DWithLifeTime : MonoBehaviour 5{ 6 [SerializeField] float _speed = 3f; 7 [SerializeField] float _lifeTime = 7f; 8 Rigidbody2D _rb; 9 Vector2 _lastCheckpoint; 10 float _timer; 11 12 void Start() 13 { 14 _lastCheckpoint = transform.position; 15 _rb = GetComponent<Rigidbody2D>(); 16 } 17 18 void Update() 19 { 20 float h = Input.GetAxisRaw("Horizontal"); 21 float v = Input.GetAxisRaw("Vertical"); 22 _rb.velocity = new Vector2(h, v) * _speed; 23 24 // 以降は「lifeTime を超えたら死亡してlastCheckpointに戻される」処理 25 _timer += Time.deltaTime; 26 27 if (_timer > _lifeTime) 28 { 29 _timer = 0; 30 Debug.Log("Dead. Move to last checkpoint."); 31 transform.position = _lastCheckpoint; 32 } 33 } 34 35 void OnTriggerEnter2D(Collider2D collision) 36 { 37 if (collision.CompareTag("Checkpoint")) 38 { 39 Debug.Log($"Enter Checkpoint: {collision.name}"); 40 _lastCheckpoint = collision.transform.position; 41 } 42 } 43}

以下は動いている様子です。

イメージ説明

投稿2022/04/21 10:05

bboydaisuke

総合スコア5275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

PlayerStartPoint = GameObject.Find("Player").transform.position;
流し見ですが、これが悪さしてないですかね?

Startの実行順はどっちが先にくるかは場合によって変わるので
SavePoint → Playermanager のStart実行順だと、Playerの初期位置で上書きして、Playerがその位置に動きます

投稿2022/04/21 09:52

hogefugapiyo

総合スコア3302

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問