- Home /
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:
http://www.youtube.com/watch?v=c27BK927-TE
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?
http://www.youtube.com/watch?v=gzdI4HmsF0Q
Please watch this video of $$anonymous$$e, hope it clear thing up.
Answer by robertbu · Mar 24, 2013 at 02:58 PM
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 $$anonymous$$d I fixed it by my own, anyway thanks for helping!
And sorry for that duplicated topic.
could you please TIC$$anonymous$$ any helpful answer. it is the TIC$$anonymous$$ symbol in a circle.
only you can close-out questions, so thanks.
Your answer
Follow this Question
Related Questions
Last time cannonball fire physics. 0 Answers
Need Javascript to propel an object toward mouse click 1 Answer
AddForce has no effect on my cannonball. 3 Answers
Bullet shots direction player is facing, cant shoot in diagnal or up 2 Answers
Make a pointer arrow that shows where player will go 1 Answer