Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/NPC-walking-fixes #364

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions Assets/GothicVR/Resources/Prefabs/Vobs/zCVobSpot.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ GameObject:
- component: {fileID: 5681108792341746749}
- component: {fileID: 4250102412515220408}
- component: {fileID: 3429925256554951498}
- component: {fileID: 5979848652310601961}
m_Layer: 2
m_Name: zCVobSpot
m_TagString: PxVob_zCVobSpot
Expand Down Expand Up @@ -98,24 +97,3 @@ MeshRenderer:
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!135 &5979848652310601961
SphereCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3434156045681339339}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Radius: 0.5
m_Center: {x: 0, y: -0.0000019073486, z: -0.000015258789}
22 changes: 0 additions & 22 deletions Assets/GothicVR/Resources/Prefabs/WayPoint.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ GameObject:
- component: {fileID: 2665078943939252271}
- component: {fileID: 1188047755481711733}
- component: {fileID: 6851058351796696224}
- component: {fileID: 3747938385529500483}
m_Layer: 2
m_Name: WayPoint
m_TagString: Untagged
Expand Down Expand Up @@ -84,24 +83,3 @@ MeshRenderer:
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!65 &3747938385529500483
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4344130464614125633}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
19 changes: 19 additions & 0 deletions Assets/GothicVR/Scripts/Debugging/GvrGizmosDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEngine;

namespace GVR.Debugging
{
/// <summary>
/// Add it to an object where you want to draw some information.
/// </summary>
public class GvrGizmosDebug : MonoBehaviour
{
private void OnDrawGizmos()
{
var pos = transform.position;
var up = new Vector3(pos.x, 100, pos.z);
var down = new Vector3(pos.x, -100, pos.z);

Gizmos.DrawLine(up, down);
}
}
}
3 changes: 3 additions & 0 deletions Assets/GothicVR/Scripts/Debugging/GvrGizmosDebug.cs.meta

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

3 changes: 3 additions & 0 deletions Assets/GothicVR/Scripts/Globals/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static class Constants
// We need to set the scale so that collision and NPC animation is starting at the right spot.
public static Vector3 VobZSScale = new(0.1f, 0.1f, 0.1f);

// e.g. for NPCs to check if they reached a FreePoint already. Value is based on best guess/testing.
public const float CloseToThreshold = 0.6f;

static Constants()
{
LoadingMaterial = new Material(ShaderWorldLit);
Expand Down
2 changes: 1 addition & 1 deletion Assets/GothicVR/Scripts/Manager/NpcHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace GVR.Manager
{
public static class NpcHelper
{
private const float fpLookupDistance = 20f; // meter
private const float fpLookupDistance = 7f; // meter

static NpcHelper()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,12 @@ public virtual void AnimationEndEventCallback(SerializableEventEndSignal eventDa

IsFinishedFlag = true;
}

public virtual void OnCollisionEnter(Collision coll)
{ }
public virtual void OnTriggerEnter(Collider coll)
{ }

public virtual void OnCollisionExit(Collision coll)
{ }
public virtual void OnTriggerExit(Collider coll)
{ }

/// <summary>
/// Called every update cycle.
/// Can be used to handle frequent things internally.
/// </summary>
public virtual void Tick(Transform transform)
public virtual void Tick()
{ }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ private string GetRotateModeAnimationString()
}
}

public override void Tick(Transform transform)
public override void Tick()
{
base.Tick(transform);
base.Tick();

HandleRotation(transform);
HandleRotation(NpcGo.transform);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using GVR.Creator;
using GVR.Data.ZkEvents;
using GVR.Globals;
using GVR.Manager;
using GVR.Vm;
using UnityEngine;
Expand Down Expand Up @@ -28,16 +29,18 @@ protected AbstractWalkAnimationAction(AnimationAction action, GameObject npcGo)
/// </summary>
protected abstract Vector3 GetWalkDestination();

protected abstract void OnDestinationReached();

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

PhysicsHelper.EnablePhysicsForNpc(Props);
}

public override void Tick(Transform transform)
public override void Tick()
{
base.Tick(transform);
base.Tick();

if (IsFinishedFlag)
return;
Expand All @@ -46,16 +49,16 @@ public override void Tick(Transform transform)
{
case WalkState.Initial:
walkState = WalkState.Rotate;
HandleRotation(transform, GetWalkDestination(), false);
HandleRotation(NpcGo.transform, GetWalkDestination(), false);
return;
case WalkState.Rotate:
HandleRotation(transform, GetWalkDestination(), false);
HandleRotation(NpcGo.transform, GetWalkDestination(), false);
return;
case WalkState.Walk:
HandleWalk(transform);
HandleWalk(Props.colliderRootMotion.transform);
return;
case WalkState.WalkAndRotate:
HandleRotation(transform, GetWalkDestination(), true);
HandleRotation(NpcGo.transform, GetWalkDestination(), true);
return;
case WalkState.Done:
return; // NOP
Expand Down Expand Up @@ -87,11 +90,21 @@ private void StartWalk()
walkState = WalkState.Walk;
}


private void HandleWalk(Transform transform)
{
// NOP
var npcPos = transform.position;
var walkPos = GetWalkDestination();
var npcDistPos = new Vector3(npcPos.x, walkPos.y, npcPos.z);

var distance = Vector3.Distance(npcDistPos, walkPos);

// FIXME - Scorpio is above FP, but values don't represent it.
if (distance < Constants.CloseToThreshold)
OnDestinationReached();
}


private void HandleRotation(Transform transform, Vector3 destination, bool includesWalking)
{
var pos = transform.position;
Expand Down
18 changes: 1 addition & 17 deletions Assets/GothicVR/Scripts/Npc/Actions/AnimationActions/GoToFp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GVR.Data.ZkEvents;
using GVR.GothicVR.Scripts.Manager;
using GVR.Manager;
using GVR.Vob.WayNet;
using UnityEngine;
Expand All @@ -23,21 +22,6 @@ public override void Start()

var npcPos = NpcGo.transform.position;
fp = WayNetHelper.FindNearestFreePoint(npcPos, destination);

// Fix - If NPC is spawned directly in front of the FP, we start transition immediately (otherwise trigger/collider won't be called).
if (Vector3.Distance(npcPos, fp!.Position) < 1f)
FreePointReached();
}

public override void OnTriggerEnter(Collider coll)
{
if (walkState != WalkState.Walk)
return;

if (coll.gameObject.name != fp.Name)
return;

FreePointReached();
}

public override void AnimationEndEventCallback(SerializableEventEndSignal eventData)
Expand All @@ -52,7 +36,7 @@ protected override Vector3 GetWalkDestination()
return fp.Position;
}

private void FreePointReached()
protected override void OnDestinationReached()
{
Props.CurrentFreePoint = fp;
fp.IsLocked = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ public override void AnimationEndEventCallback(SerializableEventEndSignal eventD

IsFinishedFlag = false;
}

protected override void OnDestinationReached()
{
AnimationEndEventCallback(new SerializableEventEndSignal(nextAnimation: ""));

walkState = WalkState.Done;
IsFinishedFlag = true;
}
}
}
31 changes: 12 additions & 19 deletions Assets/GothicVR/Scripts/Npc/Actions/AnimationActions/GoToWp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@ public override void Start()
destinationWaypoint.Name));
}

public override void OnTriggerEnter(Collider coll)
protected override Vector3 GetWalkDestination()
{
if (walkState != WalkState.Walk)
return;
return route.Peek().Position;
}

if (coll.gameObject.name != route.First().Name)
return;
public override void AnimationEndEventCallback(SerializableEventEndSignal eventData)
{
base.AnimationEndEventCallback(eventData);

// FIXME - get current waypoint object
// props.currentWayPoint = coll.gameObject.
IsFinishedFlag = false;
}

protected override void OnDestinationReached()
{
route.Pop();

if (route.Count == 0)
{
AnimationEndEventCallback(new SerializableEventEndSignal(nextAnimation: ""));

walkState = WalkState.Done;
IsFinishedFlag = true;
}
Expand All @@ -62,17 +67,5 @@ public override void OnTriggerEnter(Collider coll)
walkState = WalkState.WalkAndRotate;
}
}

protected override Vector3 GetWalkDestination()
{
return route.Peek().Position;
}

public override void AnimationEndEventCallback(SerializableEventEndSignal eventData)
{
base.AnimationEndEventCallback(eventData);

IsFinishedFlag = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ private GameObject GetNearestMobSlot(GameObject mob)
return slot;
}

public override void OnTriggerEnter(Collider coll)
protected override void OnDestinationReached()
{
if (walkState != WalkState.Walk)
return;

if (coll.gameObject != slotGo)
return;

StartMobUseAnimation();
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/GothicVR/Scripts/Npc/AiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void Start()
/// </summary>
private void Update()
{
properties.currentAction.Tick(transform);
properties.currentAction.Tick();

// Add new milliseconds when stateTime shall be measured.
if (properties.isStateTimeActive)
Expand Down
39 changes: 0 additions & 39 deletions Assets/GothicVR/Scripts/Npc/RootCollisionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
using GVR.Extensions;
using GVR.Properties;
using UnityEngine;

namespace GVR.Npc
{
public class RootCollisionHandler : BasePlayerBehaviour
{
private void OnCollisionEnter(Collision coll)
{
properties.currentAction?.OnCollisionEnter(coll);
}

private void OnTriggerEnter(Collider coll)
{
properties.currentAction?.OnTriggerEnter(coll);
}

private void OnCollisionExit(Collision coll)
{
properties.currentAction?.OnCollisionExit(coll);

// FIXME - As we handle FreePoint locking via Colliders, we can't just say "free" whenever collider is left as a rotation can create this state already.
// FIXME - Instead we need to handle unlocking via game logic. E.g. whenever a new state starts, clear our NPCs lock setting.
// // If NPC walks out of a FreePoint, it gets freed.
// if (coll.gameObject.name.StartsWithIgnoreCase("FP_") &&
// coll.gameObject.TryGetComponent<VobSpotProperties>(out var vobSpotProperties))
// vobSpotProperties.fp.IsLocked = false;
}

/// <summary>
/// Sometimes a currentAnimation needs this information. Sometimes it's just for a FreePoint to clear up.
/// </summary>
private void OnTriggerExit(Collider coll)
{
properties.currentAction?.OnTriggerExit(coll);

// FIXME - As we handle FreePoint locking via Colliders, we can't just say "free" whenever collider is left as a rotation can create this state already.
// FIXME - Instead we need to handle unlocking via game logic. E.g. whenever a new state starts, clear our NPCs lock setting.
// // If NPC walks out of a FreePoint, it gets freed.
// if (coll.gameObject.name.StartsWithIgnoreCase("FP_") &&
// coll.gameObject.TryGetComponent<VobSpotProperties>(out var vobSpotProperties))
// vobSpotProperties.fp.IsLocked = false;
}

private void Update()
{
// As we use legacy animations, we can't use RootMotion. We therefore need to rebuild it.
Expand Down