Hello guys.
Im making a 3D space ship game and i got a problem. Here’s a piece of my code:
velX+=Input.GetAxis("Horizontal")*sensStrafe;
if (Input.GetAxis("Horizontal")) {
rigidbody.AddRelativeForce(velX,0,0);
rigidbody.velocity=Vector3(Mathf.Clamp(rigidbody.velocity.x,-maxStrafe,maxStrafe),rigidbody.velocity.y,rigidbody.velocity.z);
velXdir=0;
}
else {
if (Mathf.Abs(rigidbody.velocity.x)>=0){
if (velXdir==0) {
velXdir=velX/(Mathf.Abs(velX));
}
rigidbody.AddRelativeForce(-1*stabStrafe*velXdir,0,0);
velX=0;
}
}
Im moving my ship in horizontal axis using A and D keys. I want to make an inverted force that will appear right after the strafe engines are turned off to stabilise the ship.
Variables i used and their purpose:
velX - obvious
sensStrafe - equals 1 atm.
velXdir - equals velXdir/abs(velXdir). I used that to get the dirrection of the stabilisation force.
stabStrafe - strafing speed, equals 0.5
Currently my ship got a fixed rotation and the only thing affecting it’s transform is the velX.
Problems i get:
- When im using velXdir in the rigidbody.AddRelativeForce(-1stabStrafevelXdir,0,0); im getting an error saying that the force i apply is (n/a,0,0) which means its absent as i understand it. The ship keeps strafing in the original dirrection.
- When i dont use velXdir and just put rigidbody.AddRelativeForce(-1*stabStrafe,0,0); i get another error. Since this force is applied from the fixed side, i made my ship strafe right so the stabilisation force will occure from right to left. In fact it does occure but the problem is that the stabilisation force just makes the ship endlessly strafe to the opposite direction. As i understand, it means that the if (Mathf.Abs(rigidbody.velocity.x)>=0) isnt getting the “true” value which is a mystery for me.
Can anyone help me? 
I’ll be honest and say that I didn’t follow most of that. However, this:
if (Mathf.Abs(rigidbody.velocity.x)>=0)
Looks a little suspicious. Keep in mind that Rigidbody.velocity is in world space, so the above conditional checks to see if the object is moving relative to the world x axis. Also, note that unless the object is at rest, it’s likely that conditional will almost always return true due to numerical error.
Thanks for reply Jesse.
Yes i note that rigidbody.velocity is in world space and im planing to change that in future but in the momeny the ship axis and world axis are identical since im still debugging this issue.
that if there was made to check two things:
- that the user isnt pressing A and D keys (typed wrong at first, edited, sorry). This one comes from else
- that the ship is currently moving along the x axis in any direction.
Returns true if the A and D are untouched and the ship is currently moving along the x axis in any direction (left or right).
Thats how it looks like now
if (Input.GetAxis("Horizontal")) {
rigidbody.AddRelativeForce(velX,0,0);
rigidbody.velocity=Vector3(Mathf.Clamp(rigidbody.velocity.x,-maxStrafe,maxStrafe),rigidbody.velocity.y,rigidbody.velocity.z);
velXdir=0;
}
else {
if (rigidbody.velocity.x!=0){
if (velXdir==0) {
velXdir=velX/(Mathf.Abs(velX));
}
rigidbody.AddRelativeForce(-1*stabStrafe,0,0);
velX=0;
}
}
to no avail too tho
Result: ship moving opposite after strafe but doesnt stop after velocity becomes 0.
I have no idea why this IF statement if (rigidbody.velocity.x!=0) doesnt return false. What seems suspicious to me is that when i just start the game ship doesnt move, meaning rigidbody.velocity.x=0 but after i strafe right and let go the D key it just turns on the stabilisation engine and keeps moving left even after the ship stopped. Help me >.<
lol i cant belive i was so stupid not to realise that stabStrafe is too big to make the velocity zero.
well i put this now
if (Input.GetAxis("Horizontal")) {
rigidbody.AddRelativeForce(velX,0,0);
rigidbody.velocity=Vector3(Mathf.Clamp(rigidbody.velocity.x,-maxStrafe,maxStrafe),rigidbody.velocity.y,rigidbody.velocity.z);
velXdir=0;
}
else {
if (rigidbody.velocity.x!=0){
if (velXdir==0) {
velXdir=velX/(Mathf.Abs(velX));
}
rigidbody.AddRelativeForce(-1*stabStrafe,0,0);
if (Mathf.Abs(rigidbody.velocity.x)<=(stabStrafe)) {rigidbody.velocity.x=0;}
velX=0;
}
}
i’ve read about sleepvelocity but somehow it doesnt work for me so i did it this way.
well everything works fine except for one lil problem. After the velocity became zero thanks to this if (Mathf.Abs(rigidbody.velocity.x)<=(stabStrafe)) {rigidbody.velocity.x=0;} (if i put debug.log there it says it setted velocity to 0). The new problem is that it KEEPS moving the same way but with a very small speed. That means that either velocity wasnt set 0 somehow or it was set 0 and overrided later. can someone point out a mistake? ._.