I was following the kinematic equations tutorials (
) by Sebastian Lague for a catapult like game. In his tutorial he describes that given the targets position, maximum height of the catapult’s travel arc and the gravity the ball should fly exactly onto and through the target.
But after copying his code for the xth time and trying it out myself the ball does not travel exactly through the target’s center which is basically essential for the precise catapult game.
I tried to alter Sebastian’s drawPath-function so it shows the ideal travel arc while the ball itself is already flying (I know it is not pretty, I am still learning to understand what exactly is going on). I made a screenshot before the ball hits the target and clearly the ball does not follew the ideal trajectory.
My somebody tell me what the problem is? Is there a simple solution for making the ball travel actually through the center of the target?
Sebastian is using for his example a gravity value of -18 (m/s^2). The gravity of the whole scene is put to that value in the start function. So the gravity is about twice as strong as the normal gravity in unity. But the difference between calculated and actual travel arc of the ball is somewhat minimal as shown on the picture. And I do not know why…
Post the code you use and do you have drag?
Also target height and height of initial position will play a part in any differences so please do all your tests at a fixed height for origin and target.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallLauncher_Short : MonoBehaviour
{
public Rigidbody ball;
public Transform target;
public float h = 25;
public float gravity = -18f;
// Start is called before the first frame update
void Start()
{
ball.useGravity = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Launch();
}
}
void Launch()
{
Physics.gravity = Vector3.up * gravity;
ball.useGravity = true;
ball.velocity = CalculateLauchVelocity();
print(CalculateLauchVelocity());
}
Vector3 CalculateLauchVelocity()
{
float displacementY = target.position.y - ball.position.y;
Vector3 displacementXZ = new Vector3(target.position.x - ball.position.x, 0, target.position.z - ball.position.z);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * h);
Vector3 velocityXZ = displacementXZ / (Mathf.Sqrt(-2 * h / gravity) + Mathf.Sqrt(2 * (displacementY - h) / gravity));
return velocityXZ + velocityY;
}
}
I found the github project of sebastian lague and apparently in his example the ball also misses the targets center by a bit.
I think I will try to use a different “catapult” system since I just cannot solve the difference in calculated and actual travel arc.
Why isn’t this matching the value in Physics.gravity.y ? - they both have to match, it’s not optional. Else you’ll have to apply your own custom gravity every frame.
Physics.gravity is a Vector3. So by multiplying a Vector3.up (0,1,0) with gravity (-18) we get a Physics.gravity Vector3 of (0,-18,0). Physics.gravity.y therefore is -18.