• 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 zaaKun · May 17, 2013 at 02:36 PM · c#movementparabola

Parabolic movement doing strange things with certain angles

Hello,

so I'm trying to move a Sphere or whatever in a parabolic trajectory. There is no target or anything, just gravity, angle and speed at which the sphere begins moving.

This is the code I'm using:

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class Parabola : MonoBehaviour {
     #region Public Variables
     public float gravity;
     public float angle;
     public float speed;
     public bool showVel = false;
     #endregion 
     Transform tr;
     float time;
     Vector3 pos;
     void Start() {
         tr = transform;
         pos = tr.position;
         time = 0;
         StartCoroutine("ParabolaStart");
     }
     void FixedUpdate() {
         time += Time.deltaTime;
         
     }
     IEnumerator ParabolaStart() {
         while (true) {
             float vel = (speed * Mathf.Sin(angle)) - (gravity * time);
             if (showVel)
                 print ("VEL PARABOLA: "+vel);
             float x = 0 + (speed * Mathf.Cos(angle) * time);
             float y = 0 + (speed * Mathf.Sin(angle) * time) - (0.5f * gravity * Mathf.Pow(time,2f));
             tr.position = new Vector3(x,y,pos.z);
             
             
             yield return 0;
         }
     }
     void OnCollisionEnter(Collision col) {
         print ("Collided with: "+col.transform.name);
         StopCoroutine("ParabolaStart");
     }
     
 }

The thing, it only seems to work with an angle in the 45~ range. The others I tried don't, the Sphere just slightly moves, if anything at all.

Am I missing something or doesn't it work that way in Unity?

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 Lairinus · May 17, 2013 at 03:02 PM 0
Share

Well one thing that I see that may be causing it would be that you're assigning Time.deltaTime in fixed update. Fixed Update is framerate independent, and can run multiple times or never at all, so you should try it with Time.fixedDeltaTime. Also try moving the Time.deltaTime (if you're gonna stick with it) to Update().

-Lots of the issues I encounter are because I forget to either drop the fixedDeltaTime or overuse fixedDeltaTime.

Aside from that I suck at math so I can't check your actual methodology. Just my 2c.

--I recently approached this issue differently to get a parabolic effect with raw percentages. It's slightly too spherical (still tweaking) so if this doesn't end up working out for you I can post it

1 Reply

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

Answer by robertbu · May 17, 2013 at 03:31 PM

I cannot be sure since you did not include your variable initializations, but I'll bet you are passing degrees to the Sin() and Cos() functions. They take radians. So it will be:

      float x = 0 + (speed * Mathf.Cos(angle * Mathf.Deg2Rad) * time);
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 zaaKun · May 17, 2013 at 05:02 PM 0
Share

Yes, this was the problem. Thank you

avatar image robertbu · May 17, 2013 at 05:28 PM 0
Share

If you question is answered, click the checkmark next to the answer to close it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Create bounded movement 1 Answer

Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer


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