Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by roblind3 · Oct 20, 2016 at 07:47 PM · velocityphysics2dcollision detectionaddforceangularvelocity

How do I create Sling-Shot like propulsion?

This is my first post, so please excuse any errors in my level of detail or wording. But ultimately, I am trying to accomplish this:

My character (who is on a linear path with a constant velocity, not user controlled) gets tossed into the air and will attach itself to a spinning wheel (think like a giant clock tower gear via a touch and hold control). I'm able to get this far by coding with colliders and parenting the wheel to my character...but what I have trouble with is letting go and having the character "sling-shot" off the wheel naturally, so that you take off in the direction and speed the wheel was rotating where you let go. The best example to reference would be the graspable wheels in Little Big Planet.

I have tried to research this for hours but to no avail. Any help or guidance would be greatly appreciated, thanks!

Comment
Add comment · Show 3
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 doublemax · Oct 20, 2016 at 09:23 PM 1
Share

I would try to use the current and previous position of the character to calculate a speed and direction vector.

avatar image roblind3 doublemax · Oct 21, 2016 at 08:53 PM 0
Share

@doublemax That definitely sounds like a great start. I'm still pretty new to coding though and may need some additional help if you don't $$anonymous$$d. I know how to calculate Distance using
float distance = new Vector2.Distance (currLoc, prevLoc);
therefor creating a float to use as my speed reference...but I'm stumped on how to store the previous position of my player from the last frame, and then to calculate the direction of it along both X and Y axes so that I can "shoot" the character. I'm assu$$anonymous$$g I'll use AddForce with Force$$anonymous$$ode2D.Impulse?

avatar image hexagonius roblind3 · Oct 21, 2016 at 09:13 PM 1
Share

I think, whenever you calculate your vector (you don't need just the float but a direction too, so keep the vector as a whole for the force), maybe in LateUpdate, save the currentposition as the last one. This way, when you encounter the calculation of the actual slingshot, you have both positions. Of course it will fail during the first frame, but who is able to attach and detach in one go ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by roblind3 · Oct 22, 2016 at 12:31 AM

Thanks @hexagonius. I was able to finally think up and play around with the solution! My code is pasted below, I had to manually multiply the distance and speed to seem more natural. Let me know if anyone thinks this could be simplified or made smoother. Thanks again! **On a side note, the SlingShot() function is called from another script that controls the touch gestures.

public class SlingShotScript : MonoBehaviour {

     private Rigidbody2D body;
     private Vector2 prevLoc;
     private Vector2 currLoc;
     private Vector2 distance;
     private float speed;
 
     void Awake()
     {
         prevLoc = transform.position;
     }
 
     void Start()
     {
         body = GetComponent<Rigidbody2D>();
     }
 
     public void SlingShot()
     {
         distance = (currLoc - prevLoc) * 10f;
         speed = distance.magnitude * 2f;
         body.AddForce(distance * speed, ForceMode2D.Impulse);
     }
 
     void Update()
     {
         prevLoc = transform.position;
     }
 
     void LateUpdate()
     {
         currLoc = transform.position;        
     }
 }
Comment
Add comment · Show 1 · 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 roblind3 · Oct 22, 2016 at 12:44 AM 0
Share

Ok I take it back...it does work perfectly as far as direction goes...but speed is iffy and inconsistent. Only thing I can guess is due to the way it Updates?? Sometimes it takes off, and other times it barely moves. Any thoughts?

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

78 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Determining how much force to add to reach a certain velocity in a certain amount of time and clamping it 0 Answers

Choosing speed and direction of AddForce 1 Answer

Trying to add force to an object... is this done correctly? 0 Answers

How to set rigidbody velocity and angularVelocity to Vector3.zero over time? 1 Answer

RigidBody2D AddForce Not Adding Acceleration 0 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