Target and Shoot at mouse cursor from within a Sphere

Hey guys, i am very new to unity and its been a while since i messed around with C#, but here we are anyway. I read the forums and see that this questions was asked a million times, but I still cant quite figure it out.

I have a sphere in the game, a static object not a prefab. The sphere is in constant state of rotation in various axes and speed depending on certain factors in the environment. My objective is to shoot a laser from within the sphere and use mouse coordinates as the target and multiply by some force coefficient.

APPROACH:

  1. I read about Ray Casting as a possible solution to the problem, but according to the reference it will always return positive if you shoot from within a sphere to the outside. And i am instantiating the prefab for my bullet within the sphere to match the spheres position and initial rotation. (I am not sure how to implement this correctly, is there is a way)

  2. I tried setting coordinates of the mouse pointer and adding force, by doing something along these lines:

    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
    // using Quaternion.identity because the object is in constant state of rotation, and i need the bullet to face correctly
    GameObject fireball = Instantiate (Bullet, Player.transform.position, Quaternion.identity) as GameObject;

     // Determine the mouse position and minus the Spheres position from it and add force
     fireball.rigidbody.AddForce( Input.mousePosition - Player.transform.position * 10);
    

This kind of works but not really, the sphere does shoot a bullet, BUT the bullet is not going in the right direction, even though mouse positioning does seem to affect the direction of the bullet(The bullet has a controller script that simply clamps it to Z=0 axes in every update) I tried to minus the mouse position from the players position but the result is sort of inverses and the bullet still does in wrong direction.

Apologies for asking the same question as others did a million times, but the few added variables make it difficult for me to figure out the solution, any advise on how to implement this properly is greatly appreciated.

I guess you just want the last example on the Physics.Raycast page. btw, the C# example is total rubbish.

void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 100))
        {
            Vector3 dir = (hit.point - Player.transform.position).normalized;
            GameObject fireball = (GameObject)Instantiate (Bullet, Player.transform.position, Quaternion.identity);
            fireball.rigidbody.AddForce( dir * 10, ForceMode.Impulse);
        }        
    }
}

In addition you can use Quaternion.LookRotation(dir) instead of Quaternion.identity. That way the bullets local z-axis (usually “forward”) will point along it’s movement direction.

edit

So here’s an example for a 2.5d system where every mouse click translate to a point on the x-y-plane.

private Plane m_ClickPlane;

void Start()
{
    m_ClickPlane = new Plane(-Vector3.forward, Vector3.zero);
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float dist;
        if (m_ClickPlane.Raycast(ray, out dist))
        {
            Vector3 dir = (ray.GetPoint(dist) - Player.transform.position).normalized;
            GameObject fireball = (GameObject)Instantiate (Bullet, Player.transform.position, Quaternion.identity);
            fireball.rigidbody.AddForce( dir * 10, ForceMode.Impulse);
        }        
    }
}