スマホでタップした場所に画像を表示したいです。下のコードがキャラクターにアタッチしているものなのですが実行してタップすると画像が消えてしまいます。(画面外?)しかし、適当な座標Vector3(1,1,0)とすると画像が表示されます。(Vector3の中身が間違っているのか?)Screen.widthとScreen.heightを用いて画像の表示をしたいのですがどうしたらよいのですか?よろしくお願いします。
C#
1 2コード 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class CharacterController : MonoBehaviour 8{ 9 10 // Update is called once per frame 11 void Update() 12 { 13 Vector3 Position = transform.position; 14 15 //スマホ操作 16 if (Input.touchCount > 0) 17 { 18 Touch touch = Input.GetTouch(0); 19 20 if (touch.position.x < Screen.width * 0.1f) 21 { 22 if (touch.position.y < Screen.height * 0.1f) 23 Position = new Vector3(Screen.width*0.05f, Screen.height*0.05f, 0); 24 25 else if(touch.position.y<Screen.height*0.2f) 26 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.15f, 0); 27 28 else if (touch.position.y < Screen.height * 0.3f) 29 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.25f, 0); 30 31 else if (touch.position.y < Screen.height * 0.4f) 32 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.35f, 0); 33 34 else if (touch.position.y < Screen.height * 0.5f) 35 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.45f, 0); 36 37 else if (touch.position.y < Screen.height * 0.6f) 38 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.55f, 0); 39 40 else if (touch.position.y < Screen.height * 0.7f) 41 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.65f, 0); 42 43 else if (touch.position.y < Screen.height * 0.8f) 44 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.75f, 0); 45 46 else if (touch.position.y < Screen.height * 0.9f) 47 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.85f, 0); 48 49 else 50 Position = new Vector3(Screen.width * 0.05f, Screen.height * 0.95f, 0); 51 52 } 53 transform.position = Position; 54 } 55 } 56}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。