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 Xicer · Aug 30, 2012 at 01:37 PM · anglesslingshot

Weird angles with slingshot script.

This is my slingshot script. After many days of tinkering. It is working... to an extent. But the angles that it produces are weird. If someone can help me figure out the problem i would be gratefull.

Here is the script.

 var ammo : GameObject;
 var multiplier : int;
 var spawnPosition : Vector3;
 var draggedPosition : Vector3;
 var shootVector : Vector3;
 
 rigidbody.Sleep();
 
 function Update () 
 {
 spawnPosition = ammo.transform.position;
 }
 
 
 /* function OnMouseDrag (){ammo.position = MouseCursor.mousePosition;} */
 
 function OnMouseUp ()
 {
 var draggedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 shootVector = spawnPosition.normalized - draggedPosition.normalized;
 ammo.rigidbody.AddForce(shootVector * multiplier, ForceMode.VelocityChange);
 }
Comment
Add comment · Show 1
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 Xicer · Aug 30, 2012 at 02:08 PM 0
Share

Thanks for the correction :)

1 Reply

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

Answer by Fattie · Aug 30, 2012 at 02:09 PM

Hmm, why are you "normalizing" positions? That does not make much sense, I fear.

Second ... you seem to be using "Camera.main.ScreenToWorldPoint" in one place but not in another place. You'd have to use the same approach, the same paradigm, for both points.

Thirdly ... this is more or less not how you do this sort of thing! You're trying to do it the incredibly hard way. I for instance would not be able to do it like this, too hard.

Here's all you do .. it's unbelievably simple:

one ... you see your rock object OK. write a very short script call "Move Da Rock Baby" and attach that script ("Move Da Rock Baby") to the rock.

What does "Move Da Rock Baby" do ? The ONLY THING IT DOED is allow the user to move the rock around with their finger. That's it.

(That is a very basic idea in game engineering .. you should have no trouble doing that.)

So do that and FORGET ABOUT IT!

Next! Write another simple behaviour and attach it to the rock starting position (to the slingshot "tip" OK?)

Call this one "Get Dat Angle". It is so simple it has ONE LINE OF CODE. In Update, just figure the vector of where you are minus wherever the rock is. That's it !

make the result a public variable you can see in the inspector or whatever

So you're done. AT ANY TIME you have the angle you're interested in, just grab it from the "Get Dat Angle" script at any time.

So, that's how you do it.

Never, ever try to write code in Unity - just attach trivial "behaviors" (with one or two lines of code) to game objects, and freely add game objects as needs be.

Comment
Add comment · Show 2 · 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 Xicer · Aug 30, 2012 at 02:13 PM 0
Share

I will look at this more carefully when i get home, and post the results on this page.But i can see your point. And thank you for the detailed answer.

avatar image Xicer · Aug 30, 2012 at 05:35 PM 0
Share

Well i had to learn a few new concepts to make it work. But it is working it seems. I will do some more testing and report back. And thank you again, you have been most helpful.

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

8 People are following this question.

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

Related Questions

Camera Movement and angles 2 Answers

Slingshot script. Need help troubleshooting. 1 Answer

Slingshot Based Game 1 Answer

Door script not working when door is turned 90 degrees 1 Answer

How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers


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