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
1
Question by rd42 · May 18, 2010 at 07:22 PM · transformprojectiletrajectory

Basic projectile trajectory with transform

I think I have my formulas right. I want to take a sphere g.o. use an initial x, y value and gravity to calculate launch angle and move the sphere in the typical parabolic arch across the screen. The launch angle calculates just fine, but the sphere does nothing, just sits there. Any ideas?

I press the code (101010) format button, but it only formats my first line of code?! Any ideas on that too?

var xInitial: float = 10;

var yInitial: float = 10; var x: float; var y: float; var gravity: float = -9.8; var myRigidBody: Rigidbody; var theta: float;

theta = (Mathf.Atan(xInitial / yInitial)*180)/Mathf.PI;

function FixedUpdate () {

if ( y > 0) {
x = xInitial Time.deltaTime; y = yInitial + (.5 gravity (Time.deltaTime Time.deltaTime)); transform.position = Vector3(x, y, 0); } }

function OnGUI () { GUI.Label(Rect(3,20,100,100),"theta = " + theta.ToString()); }

Comment
Add comment
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

1 Reply

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

Answer by Peter G · May 18, 2010 at 08:53 PM

I am not sure why you have to calculate the trajectory on your own. Is there something wrong with Unity's physics engine? I would try something like:

var force = 10; var myRigidBody: Rigidbody;

var angle = 50;

function Start () {

 myRigidBody = rigidbody;

}

function OnGUI () {

 if(GUI.Button(Rect(10, 10, 150, 75), "Fire")) {
     Fire ();
 }


}

function Fire () {

 transform.eulerAngles.x = -angle;
 myRigidBody.velocity = transform.forward * force;


}

and I played with your code some, and here is what I could come up with and keep your code in tact. Your problem seemed to be that you continued multiply your x and y by x/y initial which does not change, so your position would not change. Also, you might have been meaning to multiply by Time.time. Time.delta time is 1/FPS which is not the steady incrementing you want to get a consistent parabolic shape, while Time.time is a much more gradual increment.

One other thing I noticed is that there is no offset of the vertex based on position so the sphere will not travel the whole arch but you probably want to put the vertex forward of the ball's starting position, so that you can see the full parabola. I left that for you to decide how to do because there are multiple ways.

var xInitial: float = 10; var yInitial: float = 10; var x: float; var y: float; var gravity: float = -9.8; var theta: float;

function Start () {

 theta = (Mathf.Atan(xInitial / yInitial)*180)/Mathf.PI;

 x = xInitial;
 y = yInitial;

}

function FixedUpdate () {

 if ( y > 0) {
      x += Time.deltaTime; //goes up 1 every second.  Multiply to increase the speed.
      y += (.5 * gravity * (Time.deltaTime * Time.deltaTime)); // You probably either want to use x * x or Time.time * Time.time.
      transform.position = Vector3(x, y, 0); 
 } 

}

function OnGUI () { GUI.Label(Rect(3,20,100,100),"theta = " + theta.ToString()); }

Hopefully something here will help you.

Comment
Add comment · Show 7 · 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 rd42 · May 19, 2010 at 12:26 PM 0
Share

Thanks Peter G! I was using the Unity physics engine, but for the project I'm working on, I was asked to use transforms. It's a physics lesson for kids.(I probably shouldn't be the one writing it:) ) Thanks again, I'll be back in a few to add the check.

avatar image rd42 · May 19, 2010 at 01:29 PM 0
Share

Thanks again, and what is "offset of the vertex"? You are right, it doesn't ever go back down it just goes up to y initial and slowly tracks down the x coordinate. the function for displacement in the y direction y += .5t^2, should take care of that, shouldn't it? Should I ask this as another question?

avatar image rd42 · May 19, 2010 at 01:36 PM 0
Share

y does go down, I took time out of the equation and it dropped. Now I have to figure out how to get it to launch and follow the arch, ins$$anonymous$$d of going directly to it's highest point and then dropping. I should probably go back and take high school physics again too :)

avatar image Peter G · May 19, 2010 at 07:13 PM 0
Share

Well, I'm not a physics expert, but your function as you describe sounds like a linear equation. What is your equation? And by offset the vertex, I meant push it forward, so you can see the rising and the falling of the arch.

Remember high school, y = scale(x - horizonatalShift)2 + verticalShift. You want to add a horizontal shift. Your current equation takes almost no rewriting. y = .5 gravity (Time - horizontal shift) ^ 2.

avatar image rd42 · May 20, 2010 at 12:46 PM 0
Share

Thanks again for all your help. Here's what I ended up with and it appears to work based on the values I print to the console. Thanks again Peter G! x = xInitial Time.time; //yi+vi*t-1/2*g*t^2 y = (yInitial Time.time) + (.5 gravity) (Time.time * Time.time); //Print values to console for checking print ("x = " + x); print ("y = " + y);

Show more comments

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

No one has followed this question yet.

Related Questions

Projectile Trajectory Velocity, with predefined Start and End Positions 0 Answers

Confining / Constraining an object transform trajectory to a mesh 1 Answer

Trajectory on mouse up 0 Answers

Aim preview for projectile trajectory 1 Answer

Trajectory / Projectile in 3D 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