I’I have a spaceship which can lead to any place of the 3D space and this ship has a thrust.
What I’d like to do is to adjust the thrust intensity based on the facing direction of the ship and its velocity. Please note that I’m talking about the visuals, that is the particle system of the ship, and not the physics itself. That part is done already. Velocity can be found in the root RigidBody and facing direction can be calculated.
The reason for specifying all of this, is that the ship could be moving in some direction because of external forces (e.g. black hole), and this is the velocity, but the velocity might not match the direction the ship is facing, its forward.
Example 1: black hole: velocity points to black hole, direction points in the opposite, because the ship is trying to escape it.
Example 2: space battle: a ship passes by an enemy ship, and the ship might need to change its rotation (slowly) with side thrusts to face the enemy and fire.
Now, I wrote a script which is considering only the Z axis (the forward, in fact) and then the plan was to proceed adding X and Y variables, but it quickly became clear that the script was too long, and I’m sure there’s a better way of doing this, maybe involving a better use of quaternion (tbh, I don’t know how they work).
This is the uncompleted script:
public class EnergyBlast : MonoBehaviour
{
private ParticleSystem _particleSystem;
private Rigidbody _unitRigidBody;
private float _unitMaxSpeed;
[SerializeField] private float maxStartLifetime = 0.2f;
private void Start()
{
_particleSystem = ComponentUtils.RequireComponent<ParticleSystem>(this);
_unitRigidBody = ComponentUtils.RequireComponent<Rigidbody>(transform.root);
_unitMaxSpeed = ComponentUtils.RequireComponent<ShipAttributes>(transform.root).maxSpeed;
}
private void Update()
{
//Vector3 directionRotation = transform.root.transform.rotation.eulerAngles; // Equals _unitRigidBody.rotation.eulerAngles
Vector3 unitRotation = _unitRigidBody.rotation.eulerAngles;
// Ensure the direction vector is normalized
Vector3 normalizedVelocity = _unitRigidBody.velocity.normalized;
// Get the quaternion representing the rotation towards the direction vector
Quaternion rotation = Quaternion.LookRotation(normalizedVelocity);
// Convert the quaternion to Euler angles
Vector3 velocityRotation = rotation.eulerAngles;
var main = _particleSystem.main;
var abs = Math.Abs(unitRotation.z - velocityRotation.z);
if (abs < 90)
{
// Se abs = 0 --> max emissions
// Se abs = 89.9 --> zero emissions
var factor = 1 - abs/90;
Vector3 normalizedirectionVector = transform.root.transform.rotation * Vector3.forward;
float speedFactor = normalizedirectionVector.z - _unitRigidBody.velocity.z; // assume that max velocity z = 1, but it's not true
// (Blocked)
// normalizedirectionVector.z = 0 ; _unitRigidBody.velocity.z = 0, then
// normalizedirectionVector.z = 1 ; _unitRigidBody.velocity.z = 0, then
// normalizedirectionVector.z = 0 ; _unitRigidBody.velocity.z = 1, then
// normalizedirectionVector.z = 1 ; _unitRigidBody.velocity.z = 1, then
main.startLifetime = speedFactor * maxStartLifetime * factor;
}
else
{
main.startLifetime = 0;
}
}
}
I hope that someone can help me, at least pointing me in the right direction.
As you can see, I was trying not only to consider the modules of the velocity and the direction, but also the angle.

