Hi, Is there a way to target different points within a gameobject? Say, I have a large 3D model, something like a tank, ship, capital spaceship, aircraft carrier or something like that as a single gameobject. Any code that involves LookRotation for other AI objects, weapons, characters or turrets will end up aiming directly at the Gameobject’s pivot. So every shot from like say 10 guns will end up going to the same point in the centre or wherever the pivot is.
Is there any method to access various parts of the gameobject like what the red Xs in the diagram below?
Adding random offset for the shooters gun. And also make it so the aiming point would be at the center of the object.
You can get the center using Collider.bounds. For example to get the collider center point (in world space):
Collider collider = myGameObject.transform.GetComponent();
Vector3 center = collider.bounds.center;
The random offset approach will depend on how you actually do the shooting stuff, particularly how you determine the direction to shoot at. One of the options is change the shoot direction vector with Vector3.RotateTowards by rotating the vector in random direction for a random amount of degrees.
Setting up several predefined points on the object. If you need some specific points such as the object’s ‘head area’, ‘torso area’ and the object is actually just one object with one collider with no other attached objects (head, torso), then you need to determine some Vector3 headPoint, Vector3 torsoPoint. For example:
Vector3 objectPositionWorld = myObject.transform.position;
// let's say the object is 2 meters tall, the pivot is at the bottom of the object
// get the point that is 1.8 meters higher than the object pivot point, it will represent the object's head position
Vector3 headPositionWorld = myObject.transform.position + myObject.transform.up * 1.8f;
// get a position that is closer to the top right corner of the object. Assuming the objects width is 3 meters:
Vector3 topRightCornerPoint = myObject.transform.position + myObject.transform.up * 1.8f + myObject.transform.right * 2.8f;