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 TehWut · Jan 21, 2012 at 06:16 AM · 2dprojectiletrajectory

simple trajectory for rigidbody

Hello. I am trying to implement a sort of simple projectile trajectory system for my AI. Currently, it's using a quick n dirty Transform.LookAt for the targets position. I would like the upwards angle ( the x axis, it's a 2D game) to increase by X amount based on the distance from the target. I want to create a type of arch looking movement. Also, should I use addforce if I want it to be accurate? or something more like a Lerp?

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 Peter G · Jan 21, 2012 at 01:26 PM 1
Share

@Fattie, simply because Unity will handle physics for you, doesn't mean that you don't need to know how hard to throw it.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Peter G · Jan 21, 2012 at 01:25 PM

There are a number of formulas you can use for simple projectile motion. The trick is you have to figure out what variables you want to vary. If you strictly want to vary the angle then you can use one formula:

  Given:
  Distance from target = R
  angle Theta;
  Velocity.magnitude = v
  gravity = 9.8f

  v^2 * sin(2Theta) / 9.8 = R

  Solve for Theta:
  Theta = asin(dg/v^2)/2

That's about as simple as you can make it. In code terms, it looks more like this:

 var velocity : float;
 
 function Launch () {
        var theta : float = 0;
        var distance = (target.position - lauchPosition).x;

        theta = .5 * Mathf.Asin(distance * 9.8 / velocity / velocity);

        rigidbody.AddForce( velocity * new Vector3(Mathf.Cos(theta), Mathf.Sin(theta), 0) , ForceMode.VelocityChange);
 }
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 TehWut · Jan 21, 2012 at 02:43 PM 0
Share

Thank you very much! I will experiment with these today! and I'm not the best at $$anonymous$$ath so this helps :p

avatar image Peter G · Jan 21, 2012 at 07:20 PM 0
Share

After some further testing, the code works, sort of.... The current code has 2 problems. One of them is relatively easy to fix, the other is harder.

Asin always returns a value from -pi/2 to pi/2. Now, you'll notice that's always on the right side of the unit circle, meaning, it can't shoot anything to the left. There's an easy fix for this. Subtract theta from pi based upon the sign of distance (notice this is a "g" sign as opposed to sin).

The second problem is a little harder. There is a $$anonymous$$imum velocity required to reach the target. Now if you have a fixed target, that's easy to solve. But, if you target moves as I assume it does, then it requires another variable to vary which makes the math a little more difficult.

I'll see what I can come up with for the second problem. You should be able to fix the first on your own though.

avatar image Peter G · Jan 23, 2012 at 03:04 AM 0
Share

1 solution to the second problem is to set the velocity ins$$anonymous$$d of the angle. Choose the launch angle then you can figure out the velocity that will always give you that angle.

avatar image
0

Answer by ziC · Jan 21, 2012 at 04:35 PM

 // Animates the position in an arc between sunrise and sunset.
 
 var sunrise : Transform;
 var sunset : Transform;
 
 function Update () {
 // The center of the arc
 var center = (sunrise.position + sunset.position) * 0.5;
 // move the center a bit downwards to make the arc vertical
 center -= Vector3(0,1,0);
 
 // Interpolate over the arc relative to center
 var riseRelCenter = sunrise.position - center;
 var setRelCenter = sunset.position - center;
 transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, Time.time);
 transform.position += center;
 }
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 Fattie · Jan 21, 2012 at 09:35 PM 0
Share

http://unity3d.com/support/documentation/ScriptReference/Vector3.Slerp.html

this is not really related to what he is asking about.

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

6 People are following this question.

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

Related Questions

2D projectile trajectory prediction 2 Answers

Unity Messing up Basic Math? Projectile Trajectory Algorythm 1 Answer

projectile affected by parent object 1 Answer

Projectile trajectory 1 Answer

How to use RaycastHit2D for a 2D projectile? 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