# simulate motion parable

**URL:** https://discussions.unity.com/t/simulate-motion-parable/609351
**Category:** Scripting
**Tags:** Scripting
**Created:** [December 17, 2015, 8:47am UTC](https://discussions.unity.com/t/simulate-motion-parable/609351 "2015-12-17T08:47:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Bolt](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Bolt](https://discussions.unity.com/u/Bolt)
#### Post date: [December 17, 2015, 8:47am UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/1 "2015-12-17T08:47:12Z")

</div>

Hello everyone, I have tried to introduce in html and js equation to simulate the movement of a ball along a parabola with this formula: 1/2 \* g \* (x \* x) / (Vx \* Vx) + Vy / Vx \* x.  
X is the horizontal position, G is gravity, Vx is the speed on the X and Vy is the velocity on the Y. This equation works fine but I can not place it in unity. Can you give me a hand?

---

<div class="post-metadata">

### Author: ![CloudKid](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cloudkid/32/177799_2.png) [@CloudKid](https://discussions.unity.com/u/CloudKid)
#### Post date: [December 17, 2015, 9:55am UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/2 "2015-12-17T09:55:19Z")

</div>

I don’t see where is the problem, just look online how to obtain each parameter. For example x is transform.position.x.  
You take g from the rigidBody component (rigidBody.gravity?) and velosity from rigidBody.velosity. By rigid body I mean GetComponent(). If you have no idea what transform/rigidBody is I suggest to google them and learn how do they work

---

<div class="post-metadata">

### Author: ![Bolt](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Bolt](https://discussions.unity.com/u/Bolt)
#### Post date: [December 17, 2015, 12:04pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/3 "2015-12-17T12:04:42Z")

</div>

I followed a tutorial to understand trigonometry. It only works with the courutiner when the play. How can I make it work in the update?

```auto
using UnityEngine;
using System.Collections;

public class ThrowSimulation : MonoBehaviour
{
    public Transform Target;
    public float firingAngle = 45.0f;
    public float gravity = 9.8f;

    public Transform Projectile;     
    private Transform myTransform;
  
    void Awake()
    {
        myTransform = transform;     
    }

    void Update()
    {         
       SimulateProjectile();
    }

   void SimulateProjectile()
    {
        // Short delay added before Projectile is thrown
      
      
        // Move projectile to the position of throwing object + add some offset if needed.
        Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
      
        // Calculate distance to target
        float target_Distance = Vector3.Distance(Projectile.position, Target.position);

        // Calculate the velocity needed to throw the object to the target at specified angle.
        float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
      
        // Extract the X Y componenent of the velocity
        float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
        float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

        // Calculate flight time.
        float flightDuration = target_Distance / Vx;
  
        // Rotate projectile to face the target.
        Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);
      
        float elapse_time = 0;

        if(elapse_time < flightDuration)
        {
             Projectile.Translate(0, (Vy - (gravity * elapse_time))*Time.deltaTime , Vx * Time.deltaTime);
           // Projectile.Translate(0, (Vy - (gravity )) * elapse_time, Vx * Time.time);
            // Projectile.transform.position += new Vector3(0F,( Vy * elapse_time)-gravity , Vx )*Time.time ;
            elapse_time += Time.deltaTime;

        }
    
    } 
}

```

---

<div class="post-metadata">

### Author: ![CloudKid](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cloudkid/32/177799_2.png) [@CloudKid](https://discussions.unity.com/u/CloudKid)
#### Post date: [December 17, 2015, 12:09pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/4 "2015-12-17T12:09:26Z")

</div>

You have to make “elapse\_time” global and reset it (elapse\_time =0) only when you need to reset your trajectory

---

<div class="post-metadata">

### Author: ![Bolt](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Bolt](https://discussions.unity.com/u/Bolt)
#### Post date: [December 17, 2015, 12:21pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/5 "2015-12-17T12:21:46Z")

</div>

Thanks for the answer. I’m doing this exercise to understand trigonometry in video games. You know you recommend some program or plugin to help me study and understand?

---

<div class="post-metadata">

### Author: ![CloudKid](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cloudkid/32/177799_2.png) [@CloudKid](https://discussions.unity.com/u/CloudKid)
#### Post date: [December 17, 2015, 12:43pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/6 "2015-12-17T12:43:53Z")

</div>

Try out this chanel [https://www.youtube.com/channel/UCb0DBpjN2lZhJCflvMUCJpw](https://www.youtube.com/channel/UCb0DBpjN2lZhJCflvMUCJpw)  
This guy tries to explain trigonometry in Unity

---

<div class="post-metadata">

### Author: ![Bolt](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Bolt](https://discussions.unity.com/u/Bolt)
#### Post date: [December 17, 2015, 12:52pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/7 "2015-12-17T12:52:39Z")

</div>

it is yours? I am following him for some time: D

---

<div class="post-metadata">

### Author: ![CloudKid](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cloudkid/32/177799_2.png) [@CloudKid](https://discussions.unity.com/u/CloudKid)
#### Post date: [December 17, 2015, 1:34pm UTC](https://discussions.unity.com/t/simulate-motion-parable/609351/8 "2015-12-17T13:34:46Z")

</div>

Nope, cool tuts though
