Attacking different points on an object?

In the scenario I have zombies spawn and advance to a wall in order to destroy it. However I can’t seem to figure out a simple method to get them to attack any point on the wall other than the center (target.transform.position). Here’s my current code concerning looking and moving:

 void lookAt(GameObject target)
    {
       Vector3 rotation = target.transform.position - transform.position;
       Vector3 newDir = Vector3.RotateTowards(transform.forward, rotation, damping * Time.deltaTime, 0.0F);
       transform.rotation = Quaternion.LookRotation(newDir);
    }
 
 
    void moveTo (GameObject target)
    {
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); 
    }

Any way to do this without getting too complicated? Optimally I would like the ai to take up all available space on the elongated cube (wall) and when thats full be able to advance to an open spot when an enemy on the wall is shot.

1 Answer

1

One solutions is to use Collider.ClosestPointOnBounds(). Rather than use ‘target.transform.position’, you would do something like:

Vector3 pos = target.collider.ClosestPointOnBounds();
Vector3 rotation = pos - transform.position;

Note given the geometry of the situation, you may have to level ‘rotation’:

rotation.y = 0;

Can simplify it even a little more: EventSystem.current.SetSelectedGameObject(null);