teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

写真の挿入

2020/05/21 20:39

投稿

Nanmotsu
Nanmotsu

スコア22

title CHANGED
File without changes
body CHANGED
@@ -29,4 +29,8 @@
29
29
 
30
30
 
31
31
  rayをぶつけるオブジェクトをprefab化する前はうまく動いていました。
32
- それも要因かもしれません。。
32
+ それも要因かもしれません。。
33
+
34
+ Rayが当たる予定のオブジェクトのインスペクターです。
35
+
36
+ ![イメージ説明](28e143a556fcf1faa9447a05813057be.png)

1

問題点の変更

2020/05/21 20:39

投稿

Nanmotsu
Nanmotsu

スコア22

title CHANGED
@@ -1,1 +1,1 @@
1
- unityで「メソッド」がうまく動かない。
1
+ unityでprefab化したオブジェクトにRayCastがうまく作用しない。
body CHANGED
@@ -1,88 +1,32 @@
1
1
  unityで図形の「型はめゲーム」を作っています。
2
- 図形でマウスをクリック&ドラッグで動かし、ゴール近くまでいくと、位置を自動調節するメソッドを作り
2
+ 図形でマウスをクリック&ドラッグで動かし、ゴール近くまでいくと、位置を自動調節するコードを書きましたがうまく動きません。
3
- 図形本体にアタッチしているスクリプトで実行しましたが、うまく動きません。
4
3
  エラー表示などないので、どこが悪いのかわからない状態ですので、ご教授お願い致します。
5
4
 
6
5
 
7
- 以下メソッドスクリプト
6
+ 以下prefabオブジェクト側のスクリプト 修正版
8
7
  ```C#
9
- public void grphicmethod(string name1, string name2, float buzzle, Vector3 point)
10
- {
11
- if (Input.GetMouseButton(0))
8
+ if (Input.GetMouseButton(0))
12
9
  {
13
- Debug.Log("1");
10
+ Debug.Log("2");
11
+ screenPoint = Camera.main.WorldToScreenPoint(transform.position);
14
12
  Ray ray = new Ray();
13
+ Debug.Log("3");
15
14
  RaycastHit hit = new RaycastHit();
16
15
  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
17
-
16
+ Debug.Log("4");
17
+ Debug.DrawRay(ray.origin, ray.direction, Color.red);
18
+
18
19
  if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
19
20
  {
20
- Debug.Log("2");
21
+ //以降が動かない。
21
- if (hit.collider.gameObject.CompareTag(name1))
22
- {
23
- Debug.Log("3");
24
- // point = Camera.main.WorldToScreenPoint(transform.position);
25
- Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, point.z);
26
- Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint);
27
- transform.position = currentPosition;
28
- }
29
22
 
30
- }
31
- }
32
23
 
33
- if (this.transform.position.x > GameObject.FindWithTag(name2).transform.position.x - buzzle
34
- && this.transform.position.x < GameObject.FindWithTag(name2).transform.position.x + buzzle
35
- && this.transform.position.y > GameObject.FindWithTag(name2).transform.position.y - buzzle
36
- && this.transform.position.y < GameObject.FindWithTag(name2).transform.position.y + buzzle)
37
- {
38
- Debug.Log("OK");
39
- this.transform.position = GameObject.FindWithTag(name2).transform.position;
40
-
41
-
42
-
43
- }
44
-
45
24
  ```
46
25
 
47
- Debug.Log、"1"しか表示されないのそこで止まっているようで
26
+ ray可視化画像(赤色rayです
48
27
 
28
+ ![イメージ説明](5231929151044b74b1478ee9a360e002.png)
49
29
 
50
30
 
51
-
52
-
53
-
54
- 実行側スクリプトは
55
-
56
- ```C#
57
- using System.Collections;
58
- using System.Collections.Generic;
59
- using UnityEngine;
60
-
61
- public class triangleScript : MonoBehaviour
62
- {
63
-
64
- private Vector3 screenPoint;
65
-
66
- private GameControllerScript gcs;
67
-
68
-
69
-
70
- // Start is called before the first frame update
71
- void Start()
72
- {
73
-
74
- screenPoint = Camera.main.WorldToScreenPoint(transform.position);
75
-
76
- gcs = GameObject
77
- .FindWithTag("GameController")
31
+ rayをぶつけるオブジェクトをprefab化する前はうまく動いていました。
78
- .GetComponent<GameControllerScript>();
79
- }
80
-
81
- // Update is called once per frame
82
- void Update()
32
+ それも要因かもしれません。。
83
- {
84
- gcs.grphicmethod("tr", "trgoal", 0.2f, screenPoint);
85
-
86
- }
87
- }
88
- ```