Game maker orbit script
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Unity3D Orbit around orbiting object transform. RotateAround Ask Question. Asked 8 years, 4 months ago.
Active 3 years, 2 months ago. Viewed 35k times. Rotate Vector3. RotateAround target. Improve this question. Add a comment. Active Oldest Votes. Update the orbit by adding the rotation for that step: transform. Improve this answer.
Just doing that made the script I already had setup work exactly as I wanted. That is, when you rotate the planet around itself, you'll see that the moon will orbit as a result. So, you will have to do a lot of corrections.
You will have to orbit the moon with the reverse rotation of the planet, for example. By adding just one more variable direction set to 1 or -1 and multiplying it by the speed when setting the angle for RotatePointAroundPivot you can reverse the direction. Much better solution than the one Unity provides. Distance target. The name you give the script is the one you will use later as a function in code.
It is also a good idea to write some comments at the start of the script see the above image to outline the script arguments and how it should be used, especially if you are going to work as part of a team, or plan on using many scripts in your game.
Once you have done that you can start to write your script using standard GML functions and variables, using the many options along the Toolbar at the top to help you in this process. Here is a brief explanation of what each of the toolbar buttons does:. There are a number of keyboard shortcuts to help you navigate around the script editor, and each one is designed to help you get the most from this powerful tool:.
Commenting your code is very important for maintaining productivity as it keeps things clear and separates sections of code in such a way that it is obvious what each part does and why it is there. It also means that if you are working in a team then you can leave notes for other members and that they will know exactly what you have done or what a specific section of code is for.
Even if you are a solo developer, leaving comments for your code is important, especially if you have to leave the project and come back to it at a later date.
The following image illustrates a script using both these methods: If you are in the code editor, then you can also give your code box a title by making a comment for the first line using the following format:.
This comment will then be used for the "name" of that codebox this works for codeboxes added to Timelines too as illustrated by the following image: If you use make a comment in this way in the script editor, then the comment text will be used as the auto-complete text in the code editor when you type the script.
When creating larger games, you will often require many, many scripts and this can cause a certain amount of "clutter" in the resource tree, and it can be hard to keep track or organise the scripts in a coherent fashion. To help with this, the Script Editor has child scripts available to you in the form of tabs. So, say you have a player object with a create script, a movement script, a collision script and a draw script. Instead of placing them all in the resource tree, you would have a parent script, with its name being the one shown in the resource tree, and various child scripts.
These child scripts are no different to any other scripts in your game, and will work as they should. However they will not appear in the resource tree, and to access them you must first open the parent script. Here's what I would do. In oPlayer, use the built-in variables x, y, hspeed, vspeed so that GM automatically does motion based on speed.
In the step event of oPlayer, write this:. So if you hold down the right arrow, each step will add 0. Softcode this with whatever variable name you want. I tested this exact code myself in a GM example. The room speed was 60, oPlanet mass was , and things seemed to flow at a normal pace. One orbit took 1 or 2 seconds and I could easily break or re-enter orbit by pressing the arrows.
There is 1 caveat: If the player goes too close to the planet, such as sinking beneath its surface, it will get SO close to the center that gravity becomes extremely strong, which tends to make the ship go flying off in a random direction and never coming back.
To avoid this, you should make a destruction event so that the ship just crashes when it hits or goes below a planet's surface, then restart the game.
Finally, like I said, I use GM 8. I have never used GM 9, also known as GM Studio because it looks very bloated and way, way different than the nice interface of GMs 5, 6, and 8 that I am used to.
The first thing to do when you want to model space as it is is to understand Newtons laws of motion. An object keeps moving with a constant speed and direction unless a force acts on it. That means you need to get away from the methodology of moving an object by changing its position. Give an object a speed in x- and y-direction and then change its position by its current speed every frame. When you want to change the motion of the object, change the speed through acceleration and deceleration.
Now you can add a gravity source by adding another constant acceleration to it every frame in addition to the acceleration from the player input. When you want to simulate a point gravity source like a planet , you first need to make the acceleration directed into the direction of the planet.
You can do that by calculating the distance between gravity source and object with the Pythagorean law:. Then you can calculate the horizontal and vertical portion of the acceleration vector by dividing each axis distance by the total distance:. You now have a constant acceleration away from the gravity source. But you want attraction, not repelling, so you need to invert it:.
But keep in mind that in astrophysics, the magnitude of gravity is reduced by the distance squared. Also, different objects have different mass which means they are stronger gravity sources:. There's the precession problem and acceleration is much faster when approaching the object you're using as the gravitational source. This code gets rid of the precession and the velocity of the orbiting object is more manageable when approaching the planet:.
Just copy it into a script file in GameMaker and drag it into the step event of your orbiting object. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
0コメント