Hey there!
I have the following problem:
- I have a Particlesystem that shoots waterparticles with the local vel = 10
- There is a target that is not necessarily at the same height as the canon
The Gif should explain what it should be doing, even though there is no variance in the height.
So I started with this formula:
This formula calculates the needed angle with a given distance ,velocity and height, it looks like so in C#
private float FindAngleToShoot(float vel, float distance, float height) {
float g = Physics.gravity.y;
return Mathf.Atan(Mathf.Pow(vel, 2) + Mathf.Sqrt(Mathf.Pow(vel, 4) - g * (g * Mathf.Pow(distance, 2) + 2 * height * Mathf.Pow(vel, 2))) / g * distance);
}
I don’t really know why this does not work, but it did not change the angle properly.
I started to google again and found another formula, in C# like so:
private float FindAngleToShoot2(float v, float s) {
float g = Physics.gravity.y;
return Mathf.Sin(2*(Mathf.Pow(v,2) * g) / Mathf.Pow(s,2)) * Mathf.Rad2Deg;
}
Now I was able to see the angle changing its value, but it changed not properly.
It is hard to describe what actually happens, but it was “flattering” around when I moved the canon forward and backward. At some point the water really hit the fire.
So I thought about an appropiate approach and thought you guys could help me.
- find the distance to target
- be able to decide to have a high angle approach or a low angle approach
- setting the angle and velocity of the water
Shouldn’t that be all?
And I am going to attach the whole “water” class that should do all the maths.
public class Water: MonoBehaviour {
public Transform target;
private Vector3 targetPos;
public float speed = 12; //Speed of the localVelocity of the particle System
void Start() {
targetPos = target.position;
}
void Update() {
transform.localEulerAngles = new Vector3(FindAngleToShoot2(speed, Vector3.Distance(transform.position, targetPos)), 0, 0);
}
//approach 1
private float FindAngleToShoot(float vel, float distance, float height) {
float g = Physics.gravity.y;
return Mathf.Atan(Mathf.Pow(vel, 2) + Mathf.Sqrt(Mathf.Pow(vel, 4) - g * (g * Mathf.Pow(distance, 2) + 2 * height * Mathf.Pow(vel, 2))) / g * distance);
}
//approach 2
private float FindAngleToShoot2(float v, float s) {
float g = Physics.gravity.y;
return Mathf.Sin(2 * (Mathf.Pow(v, 2) * g) / Mathf.Pow(s, 2)) * Mathf.Rad2Deg;
}
Sorry for the oversized post
I really appreciate any kind of help!
Felix