Unity,transformの使い方がわかりません

実現したいこと

これを付けたオブジェクトを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 update11 void Start()12 {13 myRigidBody = this.gameObject.GetComponent<Rigidbody2D>();14 }15 16 // Update is called once per frame17void 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}

コメントを投稿

0 コメント