How to shoot to where "Mouse" is pointing?

Hello,

This is my third day in Unity. I successlly created a small game, as described in my book. The game is “shooting at bricks”. Following is the code.

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {
	
	public Rigidbody bullet;
	public float power = 1500f;
	public float moveSpeed = 25f;

	
	// Update is called once per frame
	void Update () 
	{
		float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
		float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
		
		transform.Translate(h,v,0);
		
		if(Input.GetButtonUp("Fire1"))
		{
			Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
			
			Vector3 fwd = transform.TransformDirection(Vector3.forward);
			
			instance.AddForce(fwd*power);
		}
	
	}
}

Now, the problem is, this is not shooting to where the “Mouse Pointer” is pointing. Instead, it simply start shooting from the center of the screen.

The “Shooter” is assigned to the “Main Camera” object. So, how can I shoot to the place where the mouse pointer is pointing?

Please help

This is fairly easy. change Vector3 fwd = transform.TransformDirection(Vector3.forward);
to Vector3 fwd = transform.TransformDirection(Vector3.forward) to
Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);

Distance is a seperate var as we can only retrive x an y coordinates from the mouse, so the z axis has to be put in manualy.

so the code would looke something like this

using UnityEngine;
using System.Collections;
 
public class Shooter : MonoBehaviour {
 
    public Rigidbody bullet;
    public float power = 1500f;
    public float moveSpeed = 25f;
   public float Distance=2;
 
    // Update is called once per frame
    void Update () 
    {
       float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
       float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
 
       transform.Translate(h,v,0);
 
       if(Input.GetButtonUp("Fire1"))
       {
         Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
 
       Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);

 
         instance.AddForce(fwd*power);
       }
 
    }
}

I hope this helps you get along.