-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockScript.cs
More file actions
36 lines (31 loc) · 1.11 KB
/
RockScript.cs
File metadata and controls
36 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections;
using UnityEngine;
public class RockScript : MonoBehaviour
{
private float moveSpeed;
public float collisionCheckRadius = 0.5f; // Adjust based on asteroid size
public LayerMask asteroidLayer; // Assign in Inspector to detect only asteroids
void Start()
{
moveSpeed = Random.Range(0.2f, 0.5f);
}
void Update()
{
Vector3 directionToCamera = (Camera.main.transform.position - transform.position).normalized;
// Check for another asteroid directly in the path
if (!CheckSphereAhead(directionToCamera))
{
transform.Translate(directionToCamera * Time.deltaTime * moveSpeed, Space.World);
}
else
{
// Optional: small sidestep or slow down if blocked
transform.Translate(Vector3.up * Time.deltaTime * moveSpeed, Space.World);
}
}
bool CheckSphereAhead(Vector3 direction)
{
Vector3 checkPosition = transform.position + direction * collisionCheckRadius;
return Physics.CheckSphere(checkPosition, collisionCheckRadius, asteroidLayer);
}
}