Cannonball physics?

Hi guys I want to create a game that when I click on the screen a ball goes smoothly to my mouse position, I did a little scripting and it works but what this script of mine does is when I click on the screen it is going immediately to my mouse position, what I really
want is that you have a begin position like in the middle of the screen and then whe I click it goes to my mouse position, so my mouse position is the end.

NOTE:

spot.position is the position is the begin position and pos is the end position.
I did tried Vector3.lerp and .MovePosition and I tried AddForce but with Vector3.Lerp and MovePosition it works but as i wrote before it goes immediately to the end point which is my mouse position I want that it goes like this video:

Please take a look and help me out, this is really important for me.

Thanks in advance guys!

A little script of mine:

cannonShot.Play();

        journeyLength = Vector3.Distance(spot.position, pos);

        float distCovered = (Time.time - startTime) * 20f;
        float fracJourney = distCovered / journeyLength;

        depth = 15f;

        pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(depth);

        Rigidbody ball = Instantiate(cannonBall, spot.position, Quaternion.identity) as Rigidbody;

        ball.rigidbody.MovePosition(Vector3.Lerp(spot.position, pos, fracJourney));

        //ball.rigidbody.AddForce(transform.forward * 200);
        //ball.rigidbody.AddForce(transform.up * 500);

        cannonTop.transform.LookAt(pos);

http://imageshack.us/photo/my-images/69/bombc.png/ <--- a picture from beginning project So if I click on the screen it must go to my mouse position what my script does right now is spawning the ball immediately on the mouse position what I want is that it starts form spot.position as beginnning point fire to my mouse position like cannons do. Anyway thanks for helping me out!

Yes it is 3D but it haves a 2D view, see picture --> http://imageshack.us/photo/my-images/69/bombc.png/

Can I make a video of my problem maybe it will clear thing up for you?

I already have that LOL

http://www.youtube.com/watch?v=gzdI4HmsF0Q Please watch this video of mine, hope it clear thing up.

1 Answer

1

Here is a bit of untested code to head you in the right direction. With a 3D environment when you write of “middle” and “click” you have to consider how far into the screen. This code use 10 units. You will need to adjust for whatever distance you need:

void shootInput()
{
		Vector3 v3Center = Camera.main.ViewportToWorldPoint(0.5f, 0.5f, 10.0f);
		
        cannonShot.Play();
 
        GameObject ball = Instantiate(cannonBall, v3Center, Quaternion.identity);
		
		Vector3 pos = Input.mousePosition;
		pos.z = 10;
		pos = Camera.main.ScreenToWorldPoint(pos);
		
		ball.transform.LookAt (pos);
        ball.rigidbody.AddForce(transform.forward * 2000);
 
        cannonTop.transform.LookAt(pos);
}

Never mind I fixed it by my own, anyway thanks for helping! And sorry for that duplicated topic.

could you please TICK any helpful answer. it is the TICK symbol in a circle. only you can close-out questions, so thanks.

Oke I will