Rotate Object Away From Target Within Target Script

Hello everyone, the question is simple enough but its doing my head in for some reason.

I have a number of objects in a scene, which need to rotate away from a main target object moving through the level, only this target object can have any scripts on it and the most the other objects can have is a box collider set to trigger.

So within this target object script in OnTriggerStay I need to rotate the other objects away from it on their x and z axis at their pivot point while keeping their y untouched.

Any suggestions welcome.

This question is oddly specific enough that it sounds like a homework assignment!

Anyway, OnTriggerStay gets the Collider data for the object hitting the trigger, so if I understand what you’re requesting right, a simple way you could approach it would be:

C#:

void OnTriggerStay(Collider other)
{
     Vector3 flatPosition = new Vector3(other.transform.position.x, transform.position.y, other.transform.position.z);
     Vector3 newForward = (flatPosition - transform.position).normalized;
     other.transform.forward = newForward;
}

Javascript:

function OnTriggerStay(other: Collider)
{
     var flatPosition: Vector3 = Vector3(other.transform.position.x, transform.position.y, other.transform.position.z);
     var newForward: Vector3 = (flatPosition - transform.position).normalized;
     other.transform.forward = newForward;
}

Edit: Whoops! Forgot to cancel out a rotation axis! Changed the setup accordingly.