Hi guys im trying to make a space ship that flys forward by holding space bar which makes it accelerate then decelerates when you release it, and can move round with wsad to control pitch and yaw like a real space ship. I am really struggling can anyone help?
It will help if you include some of your scripts so I and anyone else can know what level of script understanding you have. Also the type of script(JavaScript, C#, etc.) is important because most people prefer to use one type most of the time and will be helpful to you mainly just with that one.
I just answerd Kagedtiger the other day who was asking a similar question here. Only Kagedtiger wanted to swim and not go through the ground and such.
What I gave him should be a pretty good start for you (same principles), just don’t use the raycasting part and maybe change some button assignments. (Or do use the raycasting part if you don’t want to fly through space stations and such.)
motion of a real spaceship depends on much more than its thrust. inertia, relativistic effects, friction and gravity play also a big role. and this movement is hardly predictable by humans because you can’t see most of these effects.
so you would do fine when using a simpler approach.
Friction? Maybe I’m miss understanding you, but friction plays no roll in space. Theres no air, sure maybe if you enter an atmosphere (even then its just drag)… or if you wanted to consider the friction of each part of the ship, or characters inside the ship. But ignoring the inside of the ship, the ships movement is rather predictable (the physics for it was mostly developed by Newton 300 years ago), though you wouldn’t know it from a lot of games because real physics can make them some what difficult to play (kinda like the FPS character controller not using a real ridged body).
If you want it to look accurate though, all you need it gravity, inertia, and thrust. You could do all of that with any basic physics engine. I just gave a simple script that I’d basically already righten that would give basic flight. The inertia part just causes the ship to keep moving (or to sluggishly accelerate), so maybe the deceleration AnimationCurve in my script is the result of a forward engine slowing it down? (Makes game play more easy any way.) You could always fine tune the animation curves to give a more realistic effect.
As for relativistic effects, if you can think of a way to implement that in a game accurately please tell me! (seriously I’d like to implement them my self) but unless your ship is going near the speed of light it doesn’t really matter any way, because the user wont be able to see any difference at all. Even if you are going near the speed of light (and hense time around you passes faster) how would you implement that in a game?
As for gravity, well my script doesn’t take that into account, but you could always put a ridged body on it and enable gravity… or if you want planetary gravity you could have a script with a list of planetary bodies that would effect it (with enough gravity to even notice, no point in listing every visible gameobject as most wont generate enough gravity to notice) and then just add force toward each of those bodies every frame, some thing like:
var PlanitaryBodys : Ridgidbody[];
function FixedUpdate ()
{
var force = Vector3.zero;
for(var i=0; i<PlanitaryBodys.length; ++i)
{
var r = (transform.postion-PlanitaryBodys.transform.position).magnitude;
force += 6.673*rigidbody.mass*PlanitaryBodys[i].mass / Mathf.Pow(r, 2); // F=G*M1*M2/r^2
}
rigidbody.AddForce (force);
}
That will make your ship “fall” toward any bodies that you add to “PlanitaryBodys”. Just add it to the same ship and add a reference to each planet/star in a start/awake function. (Shouldn’t even effect the transform.translate in the other script I posted.) Also note that this is just like real gravity, and like real gravity your planetary bodies all have to have a good 10^10 to 10^30 times the mass of your ship for them to really have any visible effect on it. (You could always put a multiplier by the mass of the planetary body though, if you can’t get enough out of the inspector)
Thats pretty much covers it, gravity, inertia, thrust. Granted the inertia and thrust aren’t totally physically accurate, but it will give the basic idea, and you could make it look the same with a linear AnimationCurve, and just change the acceleration and deceleration floats until it speeds up and slows down how you like.
this indicates a braking force. and noone says that a spaceship never enters an atmosphere. and space is not completely empty. even radiation can influence bodies: YORP effect - Wikipedia
i have heard that such effects influence high precision sattellite measurements (gravity, gps). and they are far beyond speed of light. even space-time rotates together with planets: Frame-dragging - Wikipedia
anyway. i think irony is no irony any more when it is mentioned explicitely. i just wanted to hint the thread creator for his inappropriate phrasing.
so it should be obvious that realism is not fun in space travel. a highly simplified approach is more what the users expect.
Very true! thats why I posted the code with the animation curves, simple, shows a lot of useful concepts (timers, animations, transform.translate, and possibly transform.rotatearound). maybe they could have been a little more specific, I just didn’t pay much attention to that at the moment. (I’m on break! )
Yes, also very true! thats the exact idea behind a solar sale. They could one day replace solid and liquid fuel rockets for interplanetary space travel, but… its game design, you could try to implement exact physics with radiation and interstellar and interplanetary matter, but quite frankly the effect such matter will have on an object is so negligible that unity probably wouldn’t be able to calculate it very accurately (or quickly) with the precision of the floats provided, and it certainty wouldn’t have any great visual effect!
Yes, gravity greatly effects the functionality of those technologies, in fact I’ve referenced that when I have a speech on relativity and its implications in class. (In fact, GPS wouldn’t really even be worth it, if we didn’t account for relativistic effects.) But those effects only have to be taken into account because GPS is reliant on radio signals being transmitted between the satellites and earth (at the speed of light) so even a relatively small change in its velocity, relative to earth, has a great impact on its ability to calculate the positions of objects on earth, or near earth, based on the time it takes those signals to travel. Although, those changes in its velocity have such a minuscule effect on the difference of the rate at which time passes between them and earth, you would need to be measuring the time it takes for something moving the speed of light to travel a great distance, for relativity to make any visible difference. You might be able to measure it, but I guarantee the astronauts and cosmonauts in low earth orbit (or any potential gamer), wouldn’t see any difference in the rate some ones watch was turning on earth, unless they had an atomic clock to measure it.
That being said, I think the a AnimationCurve based script will give a good enough start. Provided of course the user doesn’t feel the need for speed!
But about the drag, yeah, I know there is a good chance the user would enter an atmosphere, but you could always just use boundaries in the game to prevent it… really depends on the type of game… if the person asking the question wants any more help with drag though I’d be more than happy to help if they ask!
sorry, missed one.
Not necessarily, as I believe I mentioned before, deceleration could be the result of an automated engine in front of the ship. Ideally you’d would want to give some form of effect for that, but you could always say the ship has a “deceleration drive” or an “inertia manipulator” of some sort, inside it.
i think that these script line are very helpful but for those people who can easily understand these lines means c#…i think that There are two space ships in circular motion (under the same gravitation field/same speed).
Suppose you can control the second space ship. You can click the button to change the velocity (in forward/backward/left/right) directions. Each click will change the same ammount of speed dv (dv can be change by slider). Default dv=2.5 and you can fire the button 10 times.
If you double dv, then you can only fire 5 times