Hi, i am adding all my script to mostParent gameobject. Then i am using this scripts respect to my objects type for example worker will do something if its collide with tree. Is it okey if i use only 1 script for collision detection. It will be more complex in the future i haven’t add health system
this is object info
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class objectInfo : MonoBehaviour
{
public bool isForChopping;
public bool isResources;
public objectType dropDown ;
public resourceType thisResourceType;
public Transform mostParent;
public Transform targetDestination;
// Start is called before the first frame update
private void Start()
{
addObjectTolist();
}
public enum objectType // your custom enumeration
{
tree,
friendUnChooseable,
friendChoosable,
enemy
};
public enum resourceType // your custom enumeration
{
wood,
};
private void OnDestroy()
{
removeObjectFromList();
}
void addObjectTolist()
{
if(isResources)
switch (thisResourceType)
{
case resourceType.wood:
allObjects.wood.addResourceToList(transform);
break;
}
}
void removeObjectFromList()
{
if(isResources)
switch (thisResourceType)
{
case resourceType.wood:
allObjects.wood.removeResourceFromList(transform);
break;
}
}
// this public var should appear as a drop down
}
this is collision detection script which added to the gameobject that has collider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class friendCollision : MonoBehaviour
{
public worker myResources;
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)
{
objectInfo myInfo;
if (other.transform.GetComponentInParent<objectInfo>() == null) return;
else myInfo = other.transform.GetComponentInParent<objectInfo>();
if (myInfo.isResources)
collisionOnResource(other, myInfo);
else if (myInfo.isForChopping)
collisionOnRawResources(myInfo);
}
void collisionOnResource(Collider other, objectInfo myInfo)
{
if (myInfo.isResources)
{
myResources.target = myInfo.targetDestination;
}
}
void collisionOnRawResources(objectInfo myInfo)
{
if (myInfo.mostParent == myResources.target)
{
myResources.myAgent.isStopped = true;
myResources.myAgent.velocity = Vector3.zero;
myResources.isCollecting = true;
}
}
}