# Newtonian physics in asteroids spin-off.

Hey all, first time poster, long time reader.
I am making an asteroids spin-off in 3d and have a question about the transform of stuff.

I have been able to make a generic script that will move the object that passes any border to the opposite side of the screen.

Now my next goal is making the speed constant (newtonian?) so that acceleration in one direction can only be countered with acceleration in the opposite direction.

I’ve got parts of this working fairly well, but there is the problem with rotation that is currently holding me back. I want the ship to keep moving in the direction I accelerated and not change direction even if I am rotating. The only valid change in direction should occur when accelerating.

How would you, dear unity users go about when writing this script? I am not using the physics engine (I think) since I’m scripting with transforms and not with forces. Should I be using vectors instead of the object’s Transform?

Thanks for reading this. I hope it makes atleast a little sense.
Steve

I’d save myself the trouble and just use the physics engine…that’s what it’s there for; no sense re-inventing the wheel. It would take about 3 lines of code that way…here’s the code for player movement in Unitroids:

``````function FixedUpdate() {
transform.Rotate(Vector3.up*(Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime) );

if (Input.GetButton("Thrust") ) {
}

transform.position.x = Mathf.Repeat(transform.position.x, fieldWidth);
transform.position.z = Mathf.Repeat(transform.position.z, fieldHeight);
}
``````

The last 2 lines make the ship wrap around the screen.

–Eriic

Hey Eric, I’m making an asteroids-esque game myself, and that wraparound code looks promising. What should I be using for the variables fieldWidth and fieldHeight (I inserted numbers for the width and height of my play area with the result of my character jumping around like crazy)?

Thanks for the fast reply! Works like a charm! I’ll be using physics in the future.

I wasn’t able to get my head around the warp function. Are the numbers supposed to be the height and width of the screen in pixels or the play area?

It’s the number of units of the play area. For example, the bottom edge is 0 and the top edge is 70, since I’m using an orthographic camera with an orthographic size of 35. The left edge is 0 and the right edge depends on the aspect ratio used.

–Eric

Thank you, you’ve been really helpful. Now I’m on to making a general firing function. I saw your “2dShooter” tutorial on the wiki, great job, you are really contributing. I’ll try to contribute as much as possible as well.

You mentioned not to invent the wheel again. You really hit the spot there. I’m having a hard time adopting to the user-friendliness of Unity3d coming directly from the once great but now forsaken environment Director. I’m not used to everything already existing and being neatly documented

Unity has become somewhat of a blissful experience to me.