How to throw a rigidbody toward the mouse?

Hi,

I’m trying to make a game, but I’m quite new to Unity and to programming. I recently got a problem with a feature of my game.

I’m making 2.5D sidescroller and I’m actually trying to create a script which will allow my character to throw a sphere in the direction of the mouse cursor, but for an unknown reason my sphere almost only move in the y axis. It slides a little bit on the x axis but no more.

Also, my sphere only move the the right, not on the left. If someone could help me on that, that would be greetly appreciated.

Here’s my code:

using UnityEngine;
using System.Collections;

public class Spherecontroller : MonoBehaviour {

	public Rigidbody Srigid;
	public float throwforce;
	private Vector3 Mposition;
	public Camera gamecamera;
	private Vector3 throwvector;


	void Start() {
		Mposition = gamecamera.ScreenToWorldPoint(Input.mousePosition);
		throwvector = new Vector3 (Mposition.x * throwforce, Mposition.y * throwforce);
	}

	void 	FixedUpdate () {
		Srigid = GetComponent<Rigidbody> ();
		if (Input.GetMouseButtonDown (0)) {
			Srigid.AddForce (throwvector);
		}
	}
}

One problem is that you only calculate MPosition in Start(). You need to calculate it just before you add force. In addition, GetMouseButtonDown() should not be called in FixedUpdate() and there is no reason to call a one-time AddForce() in FixedUpdate().