###前提・実現したいこと
プレイヤーの前にプレファブを生成したい。
現在のコードではプレイヤーのZポジション+3の位置にしか生成されないので、後ろをむいたらプレイヤーのZポジションー3のように生成する位置をかえたい。
もし、斜めの方向にも対応ができるのであれば教えていただきたいです。
###発生している問題・エラーメッセージ
プレイヤーが後ろや左右をむいてもプレイヤーのZポジション+3の位置にしか生成されない。
###該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Controller : MonoBehaviour { Animator animator; Transform transform; public GameObject p; //生成するプレファブ // Use this for initialization void Start () { animator = GetComponent<Animator> (); transform = GetComponent<Transform> (); } // Update is called once per frame void Update(){ if (Input.GetKeyDown ("space")) { animator.SetTrigger ("Jump"); animator.SetBool ("Jump2", false); } if (Input.GetKeyDown (KeyCode.M)) { animator.SetTrigger ("mahou"); animator.SetBool ("mahou2", false); Invoke ("fire", 0.5f); } } void fire(){ float X = transform.localPosition.x; float Y = transform.localPosition.y; float Z = transform.localPosition.z+3.0f; //生成位置の指定 Instantiate (p, new Vector3 (X, Y, Z), Quaternion.identity); //プレファブの生成 } }
どのようにしたらよいのか、アドバイスをお願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
Unity5.6.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/13 09:56
退会済みユーザー
2017/06/13 15:22