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

回答編集履歴

1

追記

2020/08/06 14:57

投稿

bboydaisuke
bboydaisuke

スコア5339

answer CHANGED
@@ -1,1 +1,76 @@
1
- unity 動く床 でググるといいと思います。
1
+ unity 動く床 でググるといいと思います。
2
+
3
+ 【追記】
4
+
5
+ **MovingPlatform **
6
+
7
+ ```csharp
8
+ using UnityEngine;
9
+
10
+ public class MovingPlatform : MonoBehaviour
11
+ {
12
+ [SerializeField] float m_accel = 1f;
13
+ [SerializeField] float m_magnitude = 1f;
14
+ Vector2 m_initialPosition;
15
+ float m_timer;
16
+
17
+ void Start()
18
+ {
19
+ m_initialPosition = transform.localPosition;
20
+ }
21
+
22
+ void Update()
23
+ {
24
+ m_timer += Time.deltaTime;
25
+ transform.localPosition = m_initialPosition + Mathf.Cos(m_accel * m_timer) * m_magnitude * Vector2.right;
26
+ }
27
+
28
+ void OnCollisionEnter2D(Collision2D collision)
29
+ {
30
+ if (collision.gameObject.tag == "Player")
31
+ {
32
+ collision.transform.SetParent(this.transform);
33
+ }
34
+ }
35
+
36
+ void OnCollisionExit2D(Collision2D collision)
37
+ {
38
+ if (collision.gameObject.tag == "Player")
39
+ {
40
+ collision.transform.SetParent(null);
41
+ }
42
+ }
43
+ }
44
+
45
+ ```
46
+
47
+ **PlayerWithMovingPlatform**
48
+
49
+ ```csharp
50
+ using UnityEngine;
51
+
52
+ [RequireComponent(typeof(Rigidbody2D))]
53
+ public class PlayerWithMovingPlatform : MonoBehaviour
54
+ {
55
+ [SerializeField] float m_speed = 1f;
56
+ Rigidbody2D m_rb = null;
57
+
58
+ void Start()
59
+ {
60
+ this.gameObject.tag = "Player";
61
+ m_rb = GetComponent<Rigidbody2D>();
62
+ }
63
+
64
+ void Update()
65
+ {
66
+ float h = Input.GetAxisRaw("Horizontal");
67
+ Vector2 velocity = h * m_speed * Vector2.right;
68
+ velocity.y = m_rb.velocity.y;
69
+ m_rb.velocity = velocity;
70
+ }
71
+ }
72
+ ```
73
+
74
+ **結果**
75
+
76
+ ![イメージ説明](7202f4ea280e02a4d781afdbaaf1aab9.gif)