Search Unity

Space Shooter Movement Need Code

Discussion in 'Scripting' started by MikielSG, Dec 26, 2016.

  1. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    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.
     
  2. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    Last edited: Dec 26, 2016
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,661
    @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.
     
  4. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    These reference values are outdated.

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

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    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.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,205
    No one's writing code for you. Show what you have and we'll help fix it.
     
  7. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    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.
     
  8. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    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);
    }
    }
     
  9. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    45
    Still looking for help on this problem!!!
    so Frustrated!
     
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    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 https://forum.unity3d.com/threads/using-code-tags-properly.143875/
    And finally watch some unity tutorials, they will get you started, and you will be able to write your owjn code
     
  11. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,695
    Movement:

    Rotation:


    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<Rigidbody>()" like all other components besides "transform". The error you get from using "rigidbody" tells you exactly that.
     
    Last edited: Dec 27, 2016
    theANMATOR2b likes this.
unityunity