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 /
avatar image
0
Question by santafeast · Apr 14, 2018 at 07:46 AM · rigidbody2drigidbody.addforcerigidbody.velocity

How can i initiate an object in parabolic motion without using gravity using Rigidbody2D?

Actually i want the object(attached Rigidbody2d ) to be initiated at the bottom of screen and then follow a parabolic path (generated differently using different angles) and thus hit a target which is located at center of screen? not using any gravity . i am using Rigidbody2D to this game object and would like to generate a parabolic path using physics (eg rigidbody2d.addforce or rigidbody2d.velocity) rather than Translate.

Please help me with this Regards
alt text

untitled-1.jpg (18.3 kB)
Comment
Add comment · Show 2
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 Captain_Pineapple · Apr 14, 2018 at 08:23 AM 0
Share

is the starting point always the same and do you want to create ANY parabolic path or do you only want paths that exactly end in the center position?

avatar image santafeast · Apr 16, 2018 at 06:53 AM 0
Share

No starting point changes but target is always same . I want to create a curved path similar to the image. If its possible the the gameobject follow different paths every time its initiated but hit the target.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Apr 19, 2018 at 11:00 AM

Hey there,

i did some calculations and the following should be sufficient for your problem. I assume that you have a Starting point A and a Target Point B, Scalar starting velocity v0 and an angle alpha in degrees(!).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TrajectoryScript: MonoBehaviour {
 
     public float v0;
     public float alpha;
     public float a;
     public bool start = false;
     private Transform A;
     public Transform B;
 
     public void FixedUpdate()
     {
         if (start) {
             GetComponent<Rigidbody> ().AddForce (Vector3.right * a * GetComponent<Rigidbody> ().mass);
         }
     }
 
     public void Update()
     {
         if (Input.GetKeyDown (KeyCode.Return)) {
             calculateTrajectoryValues ();
         }
     }
 
 
     public void calculateTrajectoryValues()
     {
         A = transform;
         alpha = Mathf.PI * alpha / 180.0f;
         v0 = v0 * Mathf.Sign (Mathf.Cos (alpha));
         if (Mathf.Cos (alpha) == 0) {
             alpha *= 0.99f;
         }
         a = (2 * v0 * v0 * Mathf.Pow (Mathf.Cos (alpha), 2.0f) * (B.position.x - A.position.x - (B.position.y - A.position.y) * Mathf.Tan (alpha))) / (Mathf.Pow (B.position.y - A.position.y, 2.0f));
         GetComponent<Rigidbody> ().velocity = new Vector3 (v0*Mathf.Sin(alpha),v0*Mathf.Cos(alpha));
         start = true;
     }
 }


This Script has to be attached to your object that you want to fly/throw at the target. It has to have a rigidbody attached. The public Variable B in the script has to be assigned with your targets transform.

I hope this helps (tested it and worked for me). In case you need the calculations behind this solution let me know. I can try to explain what i did and why/how it works.

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 Captain_Pineapple · Apr 20, 2018 at 07:14 AM 0
Share

If you need randomly initialized values stick to the following script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TrajectoryScript: $$anonymous$$onoBehaviour {
 
     public float v0;
     public float alpha;
     public float a;
     public bool start = false;
     private Transform A;
     public Transform B;
     public bool setRandomValues = true;
 
     public void Start()
     {
         if (setRandomValues) {
             v0 = Random.Range (1.0f, 15.0f);
             alpha = Random.Range (-80.0f, 80.0f);
         }
     }
 
     public void FixedUpdate()
     {
         if (start) {
             GetComponent<Rigidbody> ().AddForce (Vector3.right * a * GetComponent<Rigidbody> ().mass);
         }
     }
 
     public void Update()
     {
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)) {
             calculateTrajectoryValues ();
         }
     }
 
 
     public void calculateTrajectoryValues()
     {
         A = transform;
         alpha = $$anonymous$$athf.PI * alpha / 180.0f;
         v0 = v0 * $$anonymous$$athf.Sign ($$anonymous$$athf.Cos (alpha));
         if ($$anonymous$$athf.Cos (alpha) == 0) {
             alpha *= 0.99f;
         }
         a = (2 * v0 * v0 * $$anonymous$$athf.Pow ($$anonymous$$athf.Cos (alpha), 2.0f) * (B.position.x - A.position.x - (B.position.y - A.position.y) * $$anonymous$$athf.Tan (alpha))) / ($$anonymous$$athf.Pow (B.position.y - A.position.y, 2.0f));
         GetComponent<Rigidbody> ().velocity = new Vector3 (v0*$$anonymous$$athf.Sin(alpha),v0*$$anonymous$$athf.Cos(alpha));
         start = true;
     }
 }

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

How can i initiate an object in parabolic motion without using gravity using Rigidbody2D? 0 Answers

Do rigid bodies with force add force to other objects? 0 Answers

Gameobject going past target when using rigidbody2d.addforce 1 Answer

Rigidbody2D.AddForce works only once? 1 Answer

Rigidbody2D.velocity is not working. 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