Skip to content

Commit

Permalink
moving obstacle
Browse files Browse the repository at this point in the history
  • Loading branch information
bozmir committed Dec 19, 2024
1 parent f2eed35 commit 054049f
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 12 deletions.
62 changes: 58 additions & 4 deletions Assets/Scenes/Main.unity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Scripts/KerstSpecial/RaceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class RaceController : MonoBehaviour
private GameObject currentCheckpoint;
private GameObject[] zoneObjects = new GameObject[4];
private GameObject finishObject;
public GameObject scoreBoard;

private void Start()
{
Expand Down Expand Up @@ -115,6 +116,7 @@ private void OnFinish(Collider col, ZoneTrigger trigger)
{
//finish
Debug.Log("Player finished");
scoreBoard.SetActive(true);
}
}

Expand Down
29 changes: 21 additions & 8 deletions Assets/Scripts/KerstSpecial/WorldAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class WorldAsset : MonoBehaviour
public GameObject prefab;
private bool grounded = false;

protected Vector3 startPosition;
protected Coordinate startCoord;

// Start is called before the first frame update
void Start()
public virtual void Start()
{
Coordinate nextCoord = new Coordinate(CoordinateSystem.WGS84, lat, lon, 0);
Vector3 unityCoord = nextCoord.ToUnity();
startCoord = new Coordinate(CoordinateSystem.WGS84, lat, lon, 0);
Vector3 unityCoord = startCoord.ToUnity();
transform.position = unityCoord;

prefab = Instantiate(prefab);
Expand All @@ -31,18 +34,28 @@ void Start()
}

// Update is called once per frame
void Update()
public virtual void Update()
{
if (!grounded)
{
RaycastHit hit;
if (Physics.Raycast(Vector3.up * 100, Vector3.down * 1000, out hit))
RaycastHit[] hits = new RaycastHit[8];
Physics.RaycastNonAlloc(startCoord.ToUnity() + Vector3.up * 100, Vector3.down, hits, 1000);
for(int i = 0; i < hits.Length; i++)
{
Vector3 pos = prefab.transform.position;
prefab.transform.position = new Vector3(pos.x, hit.point.y, pos.z);
if (hits[i].collider is not MeshCollider)
continue;

Vector3 pos = startCoord.ToUnity();
startPosition = new Vector3(pos.x, hits[i].point.y, pos.z);
OnSetStartPosition(startPosition);
grounded = true;
}
}
}

protected virtual void OnSetStartPosition(Vector3 startPosition)
{
prefab.transform.position = startPosition;
}
}
}
64 changes: 64 additions & 0 deletions Assets/Scripts/KerstSpecial/WorldMovingAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Netherlands3D.Coordinates;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Netherlands3D.Twin
{
public class WorldMovingAsset : WorldAsset
{
public double latTarget, lonTarget;

private Vector3 target;
private bool moveToTarget = false;
public float moveSpeed = 1;

protected Coordinate targetCoord;

public override void Start()
{
base.Start();


}

protected override void OnSetStartPosition(Vector3 startPosition)
{
base.OnSetStartPosition(startPosition);
targetCoord = new Coordinate(CoordinateSystem.WGS84, latTarget, lonTarget, 0);
Vector3 unityCoord = targetCoord.ToUnity();
unityCoord.y = startPosition.y;
target = unityCoord;
}

private void UpdateCoords()
{
//startPosition =
}

public override void Update()
{
base.Update();
if (moveToTarget)
{
float dist = Vector3.Distance(prefab.transform.position, target);
if (dist > 1f)
{
prefab.transform.position = Vector3.Slerp(prefab.transform.position, target, Time.deltaTime * moveSpeed);
}
else
moveToTarget = false;
}
else
{
float dist = Vector3.Distance(prefab.transform.position, startPosition);
if (dist > 1f)
{
prefab.transform.position = Vector3.Slerp(prefab.transform.position, startPosition, Time.deltaTime * moveSpeed);
}
else
moveToTarget = true;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/KerstSpecial/WorldMovingAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 054049f

Please sign in to comment.