- Home /
Movement in space - Scrip assistance
Hello, im attempting to write a script that allows my topdown spaceship to move about as if it was in space. I.E doesnt stop when the button is released, and when rotated continues on the path it was on untill thrust is applied. So far I have this.
var ShipTopSpeed:float = 15;
var RevShipTopSpeed:float = -10;
var ShipAcceleration:float = 5;
var ShipDecceleration:float = 1.5;
var ShipCurrentSpeed:float;
var ShipRotationPosSpeed:float = 50;
var ShipRotationNegSpeed:float = -50;
var ShipRotationAcceleration:float = 5;
var ShipRotationDecceleration:float = 1;
var ShipRotationCurrentSpeed:float;
function Update () {
if(Input.GetButton(‘w’) && (ShipCurrentSpeed < ShipTopSpeed )) {
NewSpeed = ShipCurrentSpeed + ShipAcceleration;
ShipCurrentSpeed = NewSpeed;
}
if (Input.GetButton(‘d’) && (ShipRotationCurrentSpeed < ShipRotationPosSpeed )){
NewRotationSpeed = ShipRotationCurrentSpeed + ShipRotationAcceleration;
ShipRotationCurrentSpeed = NewRotationSpeed;
}
if (Input.GetButton(‘a’) && (ShipRotationCurrentSpeed > ShipRotationNegSpeed )){
NewRotationSpeed = ShipRotationCurrentSpeed – ShipRotationAcceleration;
ShipRotationCurrentSpeed = NewRotationSpeed;
}
if (Input.GetButton(‘s’) && (ShipCurrentSpeed > RevShipTopSpeed )){
NewSpeed = ShipCurrentSpeed – ShipDecceleration;
ShipCurrentSpeed = NewSpeed;
}
if (ShipCurrentSpeed > 0){
transform.Translate(Vector3.forward * Time.deltaTime * ShipCurrentSpeed );
}
if (ShipCurrentSpeed < 0) {
transform.Translate(Vector3.forward * Time.deltaTime * ShipCurrentSpeed );
}
t ransform.Rotate(Vector3.up * Time.deltaTime * ShipRotationCurrentSpeed );
if (Input.GetButtonUp(‘d’) ){
RotateSlow();
}
if (Input.GetButtonUp(‘s’) ){
RotateSlow();
}
if (!Input.GetButton(‘d’) && (!Input.GetButton(‘a’))){
if (ShipRotationCurrentSpeed != 0){
RotateSlow();
}
}
}
function RotateSlow(){
if (ShipRotationCurrentSpeed != 0){
if (ShipRotationCurrentSpeed < 0) {
NewRotationSpeed = ShipRotationCurrentSpeed + ShipRotationDecceleration;
ShipRotationCurrentSpeed = NewRotationSpeed;
}
if (ShipRotationCurrentSpeed > 0) {
NewRotationSpeed = ShipRotationCurrentSpeed – ShipRotationDecceleration;
ShipRotationCurrentSpeed = NewRotationSpeed;
}
}
}
Now this does most of what i want it to do, the ship accelerates to its top speed, and if the button is released stays at that speed. The issue starts when I add rotation to the mix. Because of the use of transform.Translate(Vector3.forward Time.deltaTime ShipCurrentSpeed ); as soon as rotation is applied, the ship shoots off in that direction. Is there a function that allows me to record the angle of thrust, and continue using that until a new thrust angle is supplied, slowly decreasing the original thrust angle until the ship is then flying in the new direction.
I'm sure there is but I cant find it, and cant work out a way of doing it with the information I currently have.
Also any simplification is welcome, this is my first bit of Javascript, and my first attempt at this type of thing.
Thanks in advance. Steve.
Answer by royce3 · Jan 09, 2012 at 11:14 PM
All of what you are wanting to do is already implemented in RigidBody, but if you want to do it yourself, here's a very simple solution:
Vector3 velocity = new Vector3();
void Update()
{
if ( Input.GetButton("w") ) velocity += transform.forward * ShipAcceleration * Time.deltaTime;
if ( Input.GetButton("s") ) velocity -= transform.forward * ShipDeceleration * Time.deltaTime;
if ( Input.GetButton("a") ) transform.RotateAround ( transform.up, -ShipRotateSpeed * Time.deltaTime );
if ( Input.GetButton("d") ) transform.RotateAround ( transform.up, ShipRotateSpeed * Time.deltaTime );
transform.position += velocity * Time.deltaTime;
}
Thanks Royce, Yeah, I found rigidbody yesterday after having another think. But as ever your above solution is more simple and elegant that my jumble of code. Thanks for taking the time to respond.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062819im_/https://answers.unity.com/themes/thub/images/avi.jpg)