- Home /
Spaceship movement script?
I currently have basic movement system for my space which is W to go forward and rotates using the mouse but doesn't feel realistic at all and is too clunky feeling. So does anyone know any good scripts I cam implement or tips that would make a good spaceship/airplane movement mechanic? Preferably third person but first person ship controls are very similar to third person anyway. Really appreciate it. If you want any more specifics then I'll add them
By the way, it's a 3rd-person ship script, it's simple and basic but it's not bad and easy to add to it. One thing that would be good is Unity's Rigidbody AddTorque, it can give your ship a realistic 'twisting' momentum on the X -axis, (Think of the old-school Asteroids arcade game).
Answer by Honorsoft · Jan 28, 2018 at 04:36 AM
I was practicing my java with a ship script, feel free to use it. I am more familiar with C# so I wasn't sure about how to use the Input.mousePosition in javascript, so you'll get an error saying:
INTERNAL_get_mousePosition is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ship_control' on game object 'Player'.
See "Script Serialization" page in the Unity Manual for further details.
But anyways, a basic, uncluttered system, and it works, I made one for C# but can't find it. Here's the javascript (ship_control.js), just add it to your 'Player' object and then attach the camera as a child of the 'Player'. You can also add a 3D model of a spaceship as a child of the 'Player' object too.
public var MousePosition = Input.mousePosition;
public var position;
public var curShipSpeed : float = 0.0;
public var step = 2;
public var current_speed = 0;
public var maxSpeed : int = 100;
public var mouse_rotate = 0.5;
public var agility = 30;
public var dist_fr_ship = 2.85;
public var rotate_speed = 2.0;
function Update () {
if (Input.GetKey ("d"))
{
transform.Rotate(0,0,-rotate_speed);
}
if (Input.GetKey ("a"))
{
transform.Rotate(0,0,rotate_speed);
}
if ((Input.GetKey("up") || Input.GetKey("w")) && (current_speed < maxSpeed)) {
current_speed += step;
} else if ((Input.GetKey("down") || Input.GetKey("s")) && (current_speed > 0)) {
current_speed -= step;
}
MousePosition = Input.mousePosition;
MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
transform.Rotate(MousePosition * Time.deltaTime * mouse_rotate, Space.Self);
curShipSpeed = Mathf.Lerp(curShipSpeed, current_speed, Time.deltaTime * step);
transform.position += transform.TransformDirection(Vector3.forward) * curShipSpeed * Time.deltaTime;
}
I think it would be better if you used velocity ins$$anonymous$$d. Example:
float acceleration = 1.5f; // change to what suits you best
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {
rigidbody.velocity += Vector3.forward * acceleration;
}
This way if you let "W" go, the ship will maintain its movement direction (exactly how space works). You will have to turn the ship and make decelerate again by pressing "W".
Rotation could work this way:
// class variables
private Vector3 rotationDirection = new Vector3();
private float roatationSpeed = 1.5f // can also change
// ----------------------- //
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
rotationDirection.y += rotationSpeed * Time.deltaTIme
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
rotationDirection.y -= rotationSpeed * Time.deltaTIme
}
transform.Rotate(rotationDirection);
This should also maintain the rotation direction until you "apply a force" in the opposite direction to stop.
Your answer
Follow this Question
Related Questions
How do I keep my character facing the direction of travel after movement stops? 1 Answer
Need help with my movement script.. :( 1 Answer
Question about moving an object technique 3 Answers
How to move the main camera in Google Cardboard? 0 Answers
Client Sprite cant be controlled when spawned by NetworkManager 1 Answer