Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by helloiam · Mar 24, 2013 at 10:26 AM · physicsdirectionsmoothshootcannonball

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);
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image helloiam · Mar 24, 2013 at 10:56 AM 0
Share

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!

avatar image helloiam · Mar 24, 2013 at 11:01 AM 0
Share

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

avatar image helloiam · Mar 24, 2013 at 11:02 AM 0
Share

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

avatar image helloiam · Mar 24, 2013 at 11:08 AM 0
Share

I already have that LOL

avatar image helloiam · Mar 24, 2013 at 11:20 AM 0
Share

http://www.youtube.com/watch?v=gzdI4HmsF0Q

Please watch this video of $$anonymous$$e, hope it clear thing up.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image helloiam · Mar 24, 2013 at 06:07 PM 0
Share

Never $$anonymous$$d I fixed it by my own, anyway thanks for helping!

And sorry for that duplicated topic.

avatar image Fattie · Mar 24, 2013 at 07:16 PM 0
Share

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.

avatar image helloiam · Mar 25, 2013 at 06:42 PM 0
Share

Oke I will

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges