Q&A
実現したいこと
これを付けたオブジェクトをplayerPositionのY座標に+5したところにワープさせたいです
前提
Unity2Dでシューティングゲームを作っています
球をspaeceが押されたらプレイヤーの前にワープさせて飛ばしたいです
playerPositionにはプレイヤーをアタッチしてあります
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Shot : MonoBehaviour 6{ 7 private Rigidbody2D myRigidBody; 8 public GameObject playerPosition; 9 public float shotSpeed = 10; 10 // Start is called before the first frame update 11 void Start() 12 { 13 myRigidBody = this.gameObject.GetComponent<Rigidbody2D>(); 14 } 15 16 // Update is called once per frame 17void FixedUpdate() 18 { 19 Vector2 force = Vector2.zero; 20 if(Input.GetKey(KeyCode.Space)) 21 { 22 transform.position = playerPosition.transform.position; 23 force = new Vector2(shotSpeed*1,0); 24 25 } 26 myRigidBody.MovePosition(myRigidBody.position + force * Time.fixedDeltaTime); 27 28 } 29}
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/02/12 11:43
2023/02/13 00:06
2023/02/13 00:06
2023/02/16 02:22
2023/02/16 10:36