Hi everyone,
I’m trying to create a space ship scene and i have some struggle trying to recreate the ship movement.
What I want is not just a simple moving with velocity, i’d like to represent the thrusters of a ship (for example 2 thruster on the back) and then “push” the ship to the forward.
To be real honest with you, i’ve managed to make it work using this code :
void Update () {
float pitch = Input.GetAxis("Pitch") * Time.deltaTime * rotationSpeed; // x
float yaw = Input.GetAxis("Yaw") * Time.deltaTime * rotationSpeed; // y
float roll = Input.GetAxis("Roll") * Time.deltaTime * rotationSpeed; // z
Quaternion AddRotation = Quaternion.identity;
AddRotation.eulerAngles = new Vector3(pitch, yaw, roll);
this._rigidbody.rotation *= AddRotation;
// Méthode 1 - OK mais pas très réaliste + si thruster détruit pas de répercussion (à part perte vitesse)
Vector3 sumForce = Vector3.zero;
for (int i = 0; i < this._motors.Count; i++) {
if (this._motors[i].getMotorState() != STATE.OFFLINE) {
sumForce += this._motors[i].getForceVector();
}
}
this._rigidbody.velocity = sumForce;
Debug.DrawLine(this.transform.position, this.transform.position + sumForce, Color.yellow);
}
And the motor code being this :
this._currentPower += Input.GetAxis("Accelerator");
this._currentPower = this._currentPower < 0 ? 0 : (this._currentPower > 100 ? 100 : this._currentPower);
UpdateMotorState(previousPower, this._currentPower);
this._forceVector = this.transform.forward * getSpeed() * Time.deltaTime;
With getSpeed being just a calcul : this._currentPower / 100 * this._maxSpeed
It look like something like that :
But the problem is when i disable one thruster, it doesn’t do what i want it to do (make the ship “rotate” because the force is suppose to push the ship from the middle-left back of the ship). So i tried to use the force physic of Unity.
I changed the code of the rigidbody velocity to this :
Vector3 sumForce = Vector3.zero;
gravityCenter = Vector3.zero;
int nbrActif = 0;
for (int i = 0; i < this._motors.Count; i++) {
if (this._motors[i].getMotorState() != STATE.OFFLINE) {
sumForce += this._motors[i].getForceVector();
gravityCenter += this._motors[i].getTransform().position;
nbrActif++;
}
}
gravityCenter /= nbrActif;
Debug.DrawLine(this.transform.position, this.transform.position + sumForce, Color.yellow);
this._rigidbody.AddForceAtPosition(sumForce, gravityCenter);
And the motor mechanic to this :
if (this._motorState != STATE.OFFLINE) {
float previousPower = this._currentPower;
this._currentPower += Input.GetAxis("Accelerator");
this._currentPower = this._currentPower < 0 ? 0 : (this._currentPower > 100 ? 100 : this._currentPower);
UpdateMotorState(previousPower, this._currentPower);
switch (this._motorState) {
case STATE.ACCELERATE:
this._forceVector = this.transform.forward * getSpeed() * Time.deltaTime;
break;
case STATE.DECELERATE:
this._forceVector = -this.transform.forward * getSpeed() * Time.deltaTime;
break;
default:
this._forceVector = Vector3.zero;
break;
}
}
else {
this._forceVector = Vector3.zero;
}
But the result is this :
What you see is the ship going forward pushed by the force, but the moment i disable a thruster, the ship start rotating (which is good !) but it suddenly stop and slide to the left.
Is it because the velocity is not correclty updated ?
Can you guys give me a tip ?
Thanks !