Skip to content

Commit 6c4e8d4

Browse files
committed
Add bobbing to pickups
1 parent 13a0721 commit 6c4e8d4

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

Assets/Project/Prefabs/AmmoPickup.prefab

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Prefabs/HealthPickup.prefab

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scenes/Game.unity

+32-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Pickup.cs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
3+
using Debug = UnityEngine.Debug;
24

35
namespace Project.Scripts
46
{
@@ -7,6 +9,39 @@ public class Pickup : MonoBehaviour
79
public PickupType type;
810
public int value = 1;
911

12+
[Header("Bobbing")]
13+
public float rotateSpeed;
14+
public float bobSpeed;
15+
public float bobHeight;
16+
17+
private Vector3 _startPosition;
18+
private bool _bobbingUp;
19+
20+
private void Start()
21+
{
22+
_startPosition = transform.position;
23+
}
24+
25+
private void Update()
26+
{
27+
var tf = transform;
28+
var pos = tf.position;
29+
30+
// Rotation.
31+
tf.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);
32+
33+
// Up/down bobbing.
34+
var offset = _bobbingUp
35+
? new Vector3(0, bobHeight * 0.5f, 0)
36+
: new Vector3(0, -bobHeight * 0.5f, 0);
37+
var target = _startPosition + offset;
38+
tf.position = pos = Vector3.MoveTowards(pos, target, bobSpeed * Time.deltaTime);
39+
if (pos == target)
40+
{
41+
_bobbingUp = !_bobbingUp;
42+
}
43+
}
44+
1045
private void OnTriggerEnter(Collider other)
1146
{
1247
if (other.CompareTag("Player"))
@@ -25,7 +60,15 @@ private void OnTriggerEnter(Collider other)
2560
player.GiveAmmo(value);
2661
break;
2762
}
63+
64+
default:
65+
{
66+
Debug.Log($"Unhandled pickup type: {type}");
67+
break;
68+
}
2869
}
70+
71+
Destroy(gameObject);
2972
}
3073
}
3174
}

0 commit comments

Comments
 (0)