The shooting functionality I have is currently spawning my projectile in the center of the player objects transform position. I want to offset that spawn position such that it actually spawns on the outside of my player objects mesh, which is currently a sphere with a radius of .5 units. This calculation must be relative to the direction that I’m firing the projectile, which is calculated by subtracting the position of the player object from the position of the target, which in the case of the player is the cursor position (converted to world coordinates) and in the case of the enemy is the position of the player.
In the past, I used a fixed “weapon” object as a spawn point. This object was rotating on a turret with a fixed length slightly longer than that of the radius of the sphere, so no calculations were necessary as the projectile would just spawn there.
After refactoring, I have gotten rid of this turret and weapon object, leaving only the player object but I need to manually calculate the new spawn position.
Here is a screencap to give you a visualization of what I need:
How do I calculate this? I’m sure there must be some simple formula or something. Here is the code if it helps:
public void Rotate(Transform target)
{
if (target)
{
_aimPosition = target.position;
_aimDirectionV3 = _aimPosition - _gameObject.transform.position;
_aimRotation = Quaternion.FromToRotation(Vector3.up, _aimDirectionV3);
}
else
{
_aimPosition = _camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
_aimDirectionV3 = _aimPosition - _gameObject.transform.position;
_aimDirectionV2 = _aimDirectionV3; // Hack for zeroing out z-axis without creating
_aimDirectionV3 = _aimDirectionV2; // new Vector3 in the middle of update loop !NO LONGER IN UPDATE LOOP!
_aimRotation = Quaternion.FromToRotation(Vector3.up, _aimDirectionV3);
}
}