実現したいこと
”CommandBall"衝突時にプレイヤーの移動方向を反転したい。(2D)
発生している問題・分からないこと
衝突判定時にspeed = -sppedとしているが、実行しても動きが反転しません。試しにspeed = -1としたところ反転しました。当方完全な初心者です。素人質問で申し訳ありませんが、どなたかご回答いただけると嬉しいです。
該当のソースコード
using UnityEngine; public class RobotController : MonoBehaviour { public float speed = 1.0f; public GameObject commandBall; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void Update() { Debug.Log(speed); transform.Translate(Vector3.right * speed); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("CommandBall")) { speed = -speed; Destroy(commandBall); } } }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
正負反転のコードなど検索しましたが、speed = -speedで良いのでは、と思い行き詰っています。
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。