Search Unity

How do you make a game object move in a parabolic trajectory using a quadratic equation?

Discussion in 'Scripting' started by boylesg, Jun 3, 2016.

  1. boylesg

    boylesg

    Joined:
    Feb 18, 2016
    Posts:
    270
    Yeah well time and experience. Like I said previously I have done very little in the way of games up till this point and therefore very little vector related maths.
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,298
  3. boylesg

    boylesg

    Joined:
    Feb 18, 2016
    Posts:
    270
    I am changing my approach to creating the sequence of movements as one ball bits another and causes it to bounce up.

    I am instantiating a new ball at the top of the ramp on each draw, and this ball will make its own way along the chain of positions without the need to pass textures along. I should be able to use OnTriggerEnter(...) to initiate an existing ball to 'get out of the way' of an arriving ball.

    This should be much simpler and easier to get a higher quality animation avoiding one sphere being superimposed on an existing sphere before it moves to the next position.

    It didn't matter when I as dealing with sprites but it looks weird with 3D spheres.
     
  4. boylesg

    boylesg

    Joined:
    Feb 18, 2016
    Posts:
    270
    Yeah I get vectors in general terms as described here, but I guess my brain wiring has not re-arranged itself sufficiently to enable me to intuitively apply what makes perfect sense to me as diagrams on paper into 0,0,0 and 1,1,1 sort of thing.
    I can do this with many aspects of programming, but not yet vector arithmetic.

    I am one very determined $ucker - I'll get there eventually.
     
    makeshiftwings likes this.
  5. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    To make an object move in the path of a quadratic equation, it's best to create a 'velocity' vector3 as your class's variable, then create a function that updates the velocity periodically using (Time.DeltaTime) based on the equation. Then create a bool such as 'isGrounded' to set true when the object has collided with something on the ground, and your updateVelocity function will check for grounded whether to update or not. You may also need a function such as 'launchObject' to get its initial velocity going, set grounded to false, then updateVelocity will handle it accordingly.

    There's a lot of things in Unity that are handled for you.. which has both positive and negative effects like a lot of things are done counter-intuitively for the sake of ease/accessibility, which takes a while to get used to even if you have tons of experience with C# and vector math. The best way to learn about this is to study the Transform component and all of its functions, as well as how Vector3s and Quaternions are used, then Colliders. For time related functionality, you will need to learn about the Time.DeltaTime and CoRoutines.

    If you haven't already, look through all of the scripting tutorials, if only for a quick glance:
    http://unity3d.com/learn/tutorials/topics/scripting
    Even though some of them are tedious and extremely easy, it will show you how Unity treats GameObjects, Components, Monobehaviors, Transforms and will eliminate a lot of confusion. There's still 1 or 2 things in there I've never fully learned, while I don't really like the videos themselves, the code examples are a nice reference, as well as the Unity Manual:
    http://docs.unity3d.com/Manual/index.html
    If you don't use both of these resources, you'll find yourself trying to 'program around' a solution that usually already exists in Unity.
    Although I strongly suggest staying away from using any of the built-in scripts such as CharacterController and FPSController, they will hurt you more than help. Creating your own code that uses Colliders,Transforms, and Rigidbodies will be better in the long run than trying to work with Character/FPSController
     
    Last edited: Jun 5, 2016
  6. boylesg

    boylesg

    Joined:
    Feb 18, 2016
    Posts:
    270
    They way I have done it in this case is to terminate the parabola when the x value of the ball reaches or surpasses the x value of the end point. Same difference I suppose.
    Waiting for the ball to bit ground or an object is problematic given that all the other elements are sprites - there are no other solid objects apart from the balls themselves.

    We decided to do ball animation with 3D objects because part of the animation involves the newly drawn bingo ball rolling down a slope. And to do this part of the animation with sprites would have required 25 (or so) * 75 individual sprite images. Rather excessive we figured. With 3D spheres we only need 75 individual ball textures - much better.
     
  7. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    48
    hey man thanks for your script, i was wondering if there is any way to shoot projectiles along the curve?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,464
    Sure. Just take the x and y values of the particles and move your projectile from one to the next.