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?
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
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?
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;
}
}
}
You have to make “elapse_time” global and reset it (elapse_time =0) only when you need to reset your trajectory
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?
Try out this chanel https://www.youtube.com/channel/UCb0DBpjN2lZhJCflvMUCJpw
This guy tries to explain trigonometry in Unity
it is yours? I am following him for some time: D
Nope, cool tuts though