Search Unity

[SOLVED]Strafe (sideways) controls + rotation control ?

Discussion in 'Scripting' started by Bolchev, Sep 3, 2015.

  1. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Hello everyone recently I've been working on my first game(space shooter) and I've published it on gamejolt. It's going great so far, but a few of the people complained that the controls are hard. As it stands my controls are setup in the following way:

    W,S - make the player move forward and backward.
    A,D - make the player rotate left/right

    Now what I want is to introduce the buttons Q and E and when pressed I want the player to move, sideways not rotate. My current code doesn't have an applied rigid body but I've tried applying one and then saying something like:
    Code (csharp):
    1.  
    2. if (Input.GetKey (KeyCode.E))
    3. {
    4. rb2d.AddForce (transform.right * speed);
    5. }
    6.  
    But that didn't do anything so here is my current code without my attempt to introduce a Rigidbody:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float maxSpeed = 5.0f;
    8.     public float rotSpeed = 140f;
    9.  
    10.     public Rigidbody2D rb2d;
    11.  
    12.     float shipBoundaryRadius = 0.5f;
    13.  
    14.     void Start () {
    15.         rb2d = gameObject.GetComponent <Rigidbody2D> ();
    16.     }
    17.  
    18.     void Update () {
    19.  
    20.         Quaternion rot = transform.rotation;
    21.  
    22.  
    23.         float z = rot.eulerAngles.z;
    24.  
    25.  
    26.  
    27.  
    28.         z -= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
    29.  
    30.         rot = Quaternion.Euler( 0, 0, z );
    31.  
    32.  
    33.         transform.rotation = rot;
    34.  
    35.  
    36.         Vector3 pos = transform.position;
    37.        
    38.         Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
    39.  
    40.         pos += rot * velocity;
    41.  
    42.         if(pos.y+shipBoundaryRadius > Camera.main.orthographicSize) {
    43.             pos.y = Camera.main.orthographicSize - shipBoundaryRadius;
    44.         }
    45.         if(pos.y-shipBoundaryRadius < -Camera.main.orthographicSize) {
    46.             pos.y = -Camera.main.orthographicSize + shipBoundaryRadius;
    47.         }
    48.  
    49.  
    50.         float screenRatio = (float)Screen.width / (float)Screen.height;
    51.         float widthOrtho = Camera.main.orthographicSize * screenRatio;
    52.  
    53.  
    54.         if(pos.x+shipBoundaryRadius > widthOrtho) {
    55.             pos.x = widthOrtho - shipBoundaryRadius;
    56.         }
    57.         if(pos.x-shipBoundaryRadius < -widthOrtho) {
    58.             pos.x = -widthOrtho + shipBoundaryRadius;
    59.         }
    60.  
    61.  
    62.         transform.position = pos;
    63.  
    64.     }
    65. }
    66.  
    P.S If you want to see the game: http://gamejolt.com/games/the-incredible-space-adventures-of-captain-alex/85320
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    After playing your game, I don't think the problem is with your controls per se, but more the physics of the game. It feels like I'm flying this ship in soup. For top-down space combat, the gold standard for the feel of controls has always been Asteroids, and there's nothing wrong with emulating that.

    For your question - I'm not really sure what you're asking. Are you asking for help in making your code use Rigidbody2D overall for movement, or are you asking how to integrate your new code with your old code?
     
  3. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Hello, first of all thank you very much for the quick response and for taking the time to test my game! I understand what you mean about the physics of the game but as this is my first game I'm not sure if I have the know-how to fix that problem :( ! I will however do a lot of research and try to add at least some sort of realism to the way objects move and interact.
    For now however a few people that have played the game have complained not so much about the physics, but rather the maneuverability of the player ship. One of them in particular requested to be able to move his ship sideways(as it stands it only rotates and moves forward and backward).
    Ideally I would like to implement two more buttons for example Q and E that add the ability for sideways controls in my current script.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You should be able to add strafing to your current code by doing the same thing you're doing for vertical input, but adding in a new value to the X component of your "velocity" vector (the first part, which is currently 0). You'll have to define a new input axis (Edit > Project Settings > input) to represent your Q and E, and use the name you define there as your input. (e.g. Input.GetAxis("Strafe") )

    (You could do this through Input.GetKey as you have in your first code block, but you generally want to use the input axes for this sort of thing. This will allow your players to customize controls and use joysticks, for example. Plus, your code is cleaner and it takes care of things like smoothing out input for you.)
     
  5. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    It absolutely worked wonders thank you very much dude! :D