Space Shooter Movement Need Code

Hello.

Looking for C# Code for ship shooter movement with the following properties.

Can move in all directions (X/Y) (Left/right is torque). No up/down movement. Just forward back.
Can turn in a complete circle if you hold down turn left/right key long enough
W for forward / Increase speed
S For backward / reduce speed
A/D for Left/right turning.

(been through a lot of various codes that do not fill these needs. most movement is left and right).

Thanks in advance.

i want the ship to move like this but all the references like “addtorque” are not valid or parsing an error.

@MikielSG

Hi there, there isn’t probably any exact cut’n’paste code available, but it seems like you need asteroids kind of controls?

Most of the things you need is in Input class and Rigidbody class.

Check the input from player, if player presses L/R keys you define, he is turning.

If he presses W or up, he wants to use engines to create thrust.

  1. Use AddForce (or other similar command from Rigidbody) along your ship transform.forward when accelerating. This way thrust will push ship to direction your ship nose is pointing.

  2. Add rotation to RigidBody of ship using AddTorque or AddRelativeTorque. Depending on direction L/R make it negative.

These reference values are outdated.

But yes, i need a asteroid style of moving (with out the edge fly threw)

I still cannot find a solution to this problem. I have spent 8 hours on this one task. Every time i get close the unity engine is not picking up the values for torque, etc.
Can someone please write this code for me in 5.5 Version of unity.

No one’s writing code for you. Show what you have and we’ll help fix it.

Iv tried 11-12 peoples version of how this should work. Not one of them works correctly. There seems to be outdated changes in the engine. Additionally, not even the website tutorial is working correctly with this.

using System.Collections;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;

void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}

Still looking for help on this problem!!!
so Frustrated!

First of all, copying code written by others isn’t a solution, because the code you mentioned earlier does indeed work, the guy just didn’t post the whole thing
Then Using code tags properly - Unity Engine - Unity Discussions
And finally watch some unity tutorials, they will get you started, and you will be able to write your owjn code

Movement:

https://www.youtube.com/watch?v=AacN8q57xJg

Rotation:

https://www.youtube.com/watch?v=aiLmUZEQBhA

To use physics do Rigidbody AddForce and AddTorque instead of using transform.

The engine doesn’t become ‘outdated’ to the point where movement and rotation stops working, so let go of that idea.

Movement and rotation are some of the most basic and fundamental concepts in Unity. There are an endless number of tutorials out there for it, and none of them are outdated. Just because a tutorial doesn’t have “asteroids” or “space shooter” in the title, doesn’t mean it won’t show you what you need to know.

In your code that you pasted, you’re using the deprecated keyword “rigidbody”. You can’t use that keyword anymore, instead you must use “GetComponent()” like all other components besides “transform”. The error you get from using “rigidbody” tells you exactly that.

1 Like