Change a Transform to Aim to the center of a Collider. Not (0,0,0)

Hi:
I am having trouble with figuring out how to change the set transform of a public transform to move towards the center of an object and not it’s (0,0,0). I would like not to add another gameobject.

Code Ex

public Transform target;
public GameObject missleSmall;

void OnFire()
{
	missleSmall.GetComponent<missleSmall>().myTarget = target;
}

“missleSmall” has a script that looksAt “myTarget” and moves forward.

This works fine in script. The problem is that the “missleSmall” goes towards the (0,0,0) of “myTarget” the feet. What I would like to do is have the “missleSmall” go towards the
center of the target, the body.

This is what I tried to do:

TR.currentTarget.localPosition = new Vector3(0,1,0);

missleSmall.GetComponent<missleSmall>().myTarget = target;

As the target size is (2,2,2) the center of it would be 1 on the Y axis. But the “target” gets moved to the world coordinates. Just wondering if there is a way to do this without adding more gameobjects to the enemy?

Short of adding a “target position” child GameObject to the enemy (which sounds the easiest solution from my POV), you should store a Vector3 offset from the enemy’s base position to the desired target position, for example in the enemy’s MonoBehaviour as a public field.

public class Enemy : MonoBehaviour {
    public Vector3 targetOffset; // set this to (0, 1, 0) in the editor
    ...
}

public class missleSmall : MonoBehaviour {
    public Enemy myTarget;

    public void Update() {
        transform.LookAt(myTarget.transform.position + myTarget.targetOffset);
        ...
    }
}

public class MyPlayer : MonoBehaviour {
    void OnFire() {
        missleSmall.GetComponent<missleSmall>().myTarget = target.GetComponent<Enemy>();
    }
}

Alternatively, you could create a separate reusable MonoBehaviour just to handle the offset, something like:

public class OffsetContainer : MonoBehaviour {
    public Vector3 offset;
}

…add that to your enemy, and retrieve that component in your code.

PS. Class names usually start with an uppercase letter, so missleSmall → MissleSmall. :slight_smile:

so there are two ways to solve this.

Either
you open the model in a modelling program and do a origin to center mass etc to move the origin of the model to it’s chest

or

you add a variable called target offset to the target that tells objects targeting it it’s center relative to it’s origin.

the thing is right now your targeting a game object not a position according to your code.
presumably at some point in the missilesmall script you actually use target.position. your going to want to use target.position + target offset to aim correctly. however creating an empty game object isn’t an issue, it’s literally a point in space. The only time it’s going to be an issue is if you use find commands to search all the game objects. Get 5000 empty objects to search and you’ll have a problem. i understand your desire to have a “clean” scene.

I suggest doing the first one, an objects position should be it’s center not it’s feet.
This can end up skewing with scaling, rotation, position (as you’ve just seen)
unity has boxes and such origin’d to there center, keep with the standard.