回答編集履歴

1

追記

2020/08/06 14:57

投稿

bboydaisuke
bboydaisuke

スコア5308

test CHANGED
@@ -1 +1,151 @@
1
1
  unity 動く床 でググるといいと思います。
2
+
3
+
4
+
5
+ 【追記】
6
+
7
+
8
+
9
+ **MovingPlatform **
10
+
11
+
12
+
13
+ ```csharp
14
+
15
+ using UnityEngine;
16
+
17
+
18
+
19
+ public class MovingPlatform : MonoBehaviour
20
+
21
+ {
22
+
23
+ [SerializeField] float m_accel = 1f;
24
+
25
+ [SerializeField] float m_magnitude = 1f;
26
+
27
+ Vector2 m_initialPosition;
28
+
29
+ float m_timer;
30
+
31
+
32
+
33
+ void Start()
34
+
35
+ {
36
+
37
+ m_initialPosition = transform.localPosition;
38
+
39
+ }
40
+
41
+
42
+
43
+ void Update()
44
+
45
+ {
46
+
47
+ m_timer += Time.deltaTime;
48
+
49
+ transform.localPosition = m_initialPosition + Mathf.Cos(m_accel * m_timer) * m_magnitude * Vector2.right;
50
+
51
+ }
52
+
53
+
54
+
55
+ void OnCollisionEnter2D(Collision2D collision)
56
+
57
+ {
58
+
59
+ if (collision.gameObject.tag == "Player")
60
+
61
+ {
62
+
63
+ collision.transform.SetParent(this.transform);
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ void OnCollisionExit2D(Collision2D collision)
72
+
73
+ {
74
+
75
+ if (collision.gameObject.tag == "Player")
76
+
77
+ {
78
+
79
+ collision.transform.SetParent(null);
80
+
81
+ }
82
+
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+ ```
90
+
91
+
92
+
93
+ **PlayerWithMovingPlatform**
94
+
95
+
96
+
97
+ ```csharp
98
+
99
+ using UnityEngine;
100
+
101
+
102
+
103
+ [RequireComponent(typeof(Rigidbody2D))]
104
+
105
+ public class PlayerWithMovingPlatform : MonoBehaviour
106
+
107
+ {
108
+
109
+ [SerializeField] float m_speed = 1f;
110
+
111
+ Rigidbody2D m_rb = null;
112
+
113
+
114
+
115
+ void Start()
116
+
117
+ {
118
+
119
+ this.gameObject.tag = "Player";
120
+
121
+ m_rb = GetComponent<Rigidbody2D>();
122
+
123
+ }
124
+
125
+
126
+
127
+ void Update()
128
+
129
+ {
130
+
131
+ float h = Input.GetAxisRaw("Horizontal");
132
+
133
+ Vector2 velocity = h * m_speed * Vector2.right;
134
+
135
+ velocity.y = m_rb.velocity.y;
136
+
137
+ m_rb.velocity = velocity;
138
+
139
+ }
140
+
141
+ }
142
+
143
+ ```
144
+
145
+
146
+
147
+ **結果**
148
+
149
+
150
+
151
+ ![イメージ説明](7202f4ea280e02a4d781afdbaaf1aab9.gif)