Search Unity

Movement system for 3d space shooter

Discussion in 'Scripting' started by Zaboleq, Nov 28, 2012.

  1. Zaboleq

    Zaboleq

    Joined:
    Nov 1, 2012
    Posts:
    21
    Hello,
    I created simple 2d space shooter where my object only move along x and z axis and rotate along Y axis towards mouse cursor(I used LookAtMouse script in C# from wiki).

    But right now Im thinking about creating 3d space shooter, where my ship can move in 3d space environment, like in those games:
    - http://www.youtube.com/watch?v=7V_qXyNKHgc
    or
    -http://www.youtube.com/watch?v=nQpOgAA21oE (this is better I think:p)

    But I dont know where to start unfortunately... Do you know anything that could help me? Or I need to buy some projects in Asset Store and look at source code?
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    There are a many free scripts on the web, also free projects to get your hands on. For anyone new to Unity it's a good idea to just download everything Unity. All tutorials, scripts and other resources. Then go through the tutorials to see how things work. This builds a nice library of scripts to reference from in the future.

    Space games don't have gravity ( most the time lol) so a rigidbody with its gravity turned off and the use of rigidbody add force is one way to go. Then all you need is the smooth camera script that comes with Unity.
     
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    You can play whit this script , i use it a lot in most occasions. Hope help you.

    Code (csharp):
    1. #pragma strict
    2. // velocidade do movimento do jogador para cima.
    3. var jogadorCima : int = 5;
    4. // velocidade do movimento do jogador para baixo.
    5. var jogadorBaixo : int = 5;
    6. // velocidade do movimento do jogador para Direita.
    7. var jogadorDireita : int = 5;
    8. // velocidade do movimento do jogador para Esquerda.
    9. var jogadorEsquerda : int = 3;
    10. // velocidade do movimento do jogador para Direita automático pelo nível.
    11. var velocidaFrontal : float = 0.05;
    12. var maximoPontos : int = 100;
    13. static var pontosActual = 0;
    14. static var vidas : int = 3;
    15. // acções tiros
    16. var tiro1a : Rigidbody;
    17. //var activo : int = 1;
    18. var bulletTotal : int = 50;
    19. function Start ()
    20. {
    21.  // Check and get the current points saved data.
    22.  pontosActual = PlayerPrefs.GetInt("Pontos");
    23. }
    24. function Update ()
    25. {
    26.  if (Input.GetKey(KeyCode.W))
    27.  {
    28.   // Move o jogador para cima.
    29.   transform.Translate(0,0,1 * jogadorCima * Time.deltaTime);  
    30.  }
    31.  
    32.  if (Input.GetKey(KeyCode.S))
    33.  {
    34.   // Move o jogador para baixo.
    35.   transform.Translate(0,0,1 * -jogadorBaixo * Time.deltaTime);
    36.  }
    37.  
    38.  if (Input.GetKey(KeyCode.D))
    39.  {
    40.   // Move o jogador para Direita.
    41.   transform.Translate(1 * jogadorDireita * Time.deltaTime,0,0);  
    42.   transform.Rotate(0,1 * 100 * Time.deltaTime, 0);
    43.  }
    44.  
    45.  if (Input.GetKey(KeyCode.A))
    46.  {
    47.   // Move o jogador para esquerda.
    48.   transform.Translate(1 * -jogadorEsquerda * Time.deltaTime,0,0);  
    49.   transform.Rotate(0,1 * -100 * Time.deltaTime, 0);
    50.  }
    51.  
    52.  // controla o arranque da nave e paragem.
    53.  //if (activo)
    54.  //{
    55.   // Move a nave automatico pelo nivel.
    56.   //transform.Translate(0,0,1 * velocidaFrontal);
    57.  //}
    58.  
    59.  
    60.  // Controlo das armas.
    61.  // Disparo standard
    62.  if (Input.GetKeyDown(KeyCode.Space)  bulletTotal)
    63.  {
    64.   var tempTiro1a : Rigidbody;
    65.  
    66.   tempTiro1a = Instantiate(tiro1a, transform.position, transform.rotation);
    67.  
    68.   bulletTotal --;
    69.  
    70.   if (bulletTotal == 0)
    71.   {  
    72.    bulletTotal += 100;
    73.   }
    74.  }
    75.  
    76.  //****************************
    77.  if (Input.GetKey(KeyCode.Alpha1))
    78.  {
    79.   Debug.Log("Arma 1 activa");
    80.  }
    81.  
    82.  if (Input.GetKey(KeyCode.Alpha2))
    83.  {
    84.   Debug.Log("Arma 2 activa");
    85.  }
    86.  
    87.  if (Input.GetKey(KeyCode.Alpha3))
    88.  {
    89.   Debug.Log("Arma 3 activa");
    90.  }
    91.  
    92.  if (Input.GetKey(KeyCode.Alpha4))
    93.  {
    94.   Debug.Log("Arma 4 activa");
    95.  }
    96.  
    97.  // Controlo de pontos
    98.  if (pontosActual < 0)
    99.  {
    100.   pontosActual = 0;
    101.  }
    102.  
    103.  if (pontosActual > maximoPontos)
    104.  {
    105.   pontosActual = maximoPontos;
    106.  }
    107.  
    108.  if (pontosActual < 0)
    109.  {
    110.   pontosActual = 0;
    111.  }
    112.  
    113.  // Verifica se está no máximo de pontos
    114.  if (pontosActual >= maximoPontos)
    115.  {
    116.   Debug.Log("Full Points");
    117.  }
    118.  
    119. }
    120. function OnGUI()
    121. {    
    122.     GUI.Label(Rect(10,10,200,50),"Pontos: " + pontosActual);  
    123.     GUI.Label(Rect(10,30,200,50),"Vidas: " + vidas);    
    124. }
    125. @script ExecuteInEditMode()
     
    Last edited: Nov 28, 2012
  4. Zaboleq

    Zaboleq

    Joined:
    Nov 1, 2012
    Posts:
    21
    This script is not written in english lol :E...


    Also, even if I will use rigibody, how Can I rotate my ship with camera. Is there any tutorial for that?
    I mean that I wanna do something like that my ship will follow my camera and rotate with my mouse?

    For example:
    I press "W" key so my ship is start moving ahead, then I use my mouse to rotate my ship so he can get to the higher point.
     
  5. Scobbo

    Scobbo

    Joined:
    Aug 18, 2012
    Posts:
    43
    Hey Zoboleq, I'd suggest using the "Space Game Starter Kit" It is really easy if you are willing to look through the code. Completely modular and very simple to add new modules for. It is on the asset store by a group called Tasharen, same mob who made the NGUI. Helped me out a lot.
     
  6. Zaboleq

    Zaboleq

    Joined:
    Nov 1, 2012
    Posts:
    21
    I saw that once, but it is to expensive for me and I think that I prefer self-learning method through tutorials. Unfortunately there is lack of this kind tutorials...
     
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    English version.

    Code (csharp):
    1. #pragma strict
    2. // movement speed of the player upwards.
    3. var playerUp : int = 5;
    4. // movement speed of the player down.
    5. var playerdown : int = 5;
    6. // movement speed of the player to the right.
    7. var playerRight : int = 5;
    8. // movement speed of the player to left.
    9. var playerleft : int = 3;
    10. // movement speed of the player to the right automatic level.
    11. var frontalSpeed : float = 0.05;
    12. var maxPoints : int = 100;
    13. static var curPoints = 0;
    14. static var Life : int = 3;
    15. // acções tiros
    16. var tiro1a : Rigidbody;
    17. //var activo : int = 1;
    18. var bulletTotal : int = 50;
    19. function Start ()
    20. {
    21.  // Check and get the current points saved data.
    22.  curPoints = PlayerPrefs.GetInt("Points");
    23. }
    24. function Update ()
    25. {
    26.  if (Input.GetKey(KeyCode.W))
    27.  {
    28.   // Move the player up.
    29.   transform.Translate(0,0,1 * playerUp * Time.deltaTime);  
    30.  }
    31.  
    32.  if (Input.GetKey(KeyCode.S))
    33.  {
    34.   // Move the player down.
    35.   transform.Translate(0,0,1 * -playerdown * Time.deltaTime);
    36.  }
    37.  
    38.  if (Input.GetKey(KeyCode.D))
    39.  {
    40.   // Move the player to Right.
    41.   transform.Translate(1 * playerRight * Time.deltaTime,0,0);  
    42.   transform.Rotate(0,1 * 100 * Time.deltaTime, 0);
    43.  }
    44.  
    45.  if (Input.GetKey(KeyCode.A))
    46.  {
    47.   // Move the player to left.
    48.   transform.Translate(1 * -playerleft * Time.deltaTime,0,0);  
    49.   transform.Rotate(0,1 * -100 * Time.deltaTime, 0);
    50.  }
    51.  
    52.  // controls the starting and stopping of the ship.
    53.  //if (activo)
    54.  //{
    55.   // Move the ship automatically by level.
    56.   //transform.Translate(0,0,1 * frontalSpeed);
    57.  //}
    58.  
    59.  
    60.  // Arms Control.
    61.  // Shooting standard
    62.  if (Input.GetKeyDown(KeyCode.Space)  bulletTotal)
    63.  {
    64.   var tempTiro1a : Rigidbody;
    65.  
    66.   tempTiro1a = Instantiate(tiro1a, transform.position, transform.rotation);
    67.  
    68.   bulletTotal --;
    69.  
    70.   if (bulletTotal == 0)
    71.   {  
    72.    bulletTotal += 100;
    73.   }
    74.  }
    75.  
    76.  //**************************** F1, F2, F3,F4 ***************************************
    77.  if (Input.GetKey(KeyCode.Alpha1))
    78.  {
    79.   Debug.Log("Weapon 1 active");
    80.  }
    81.  
    82.  if (Input.GetKey(KeyCode.Alpha2))
    83.  {
    84.   Debug.Log("Weapon 2 active");
    85.  }
    86.  
    87.  if (Input.GetKey(KeyCode.Alpha3))
    88.  {
    89.   Debug.Log("Weapon 3 active");
    90.  }
    91.  
    92.  if (Input.GetKey(KeyCode.Alpha4))
    93.  {
    94.   Debug.Log("Weapon 4 active");
    95.  }
    96.  
    97.  // Control points
    98.  if (curPoints < 0)
    99.  {
    100.   curPoints = 0;
    101.  }
    102.  
    103.  if (curPoints > maxPoints)
    104.  {
    105.   curPoints = maxPoints;
    106.  }
    107.  
    108.  if (curPoints < 0)
    109.  {
    110.   curPoints = 0;
    111.  }
    112.  
    113.  // Checks whether this in maximum points
    114.  if (curPoints >= maxPoints)
    115.  {
    116.   Debug.Log("Full Points");
    117.  }
    118.  
    119. }
    120. function OnGUI()
    121. {    
    122.     GUI.Label(Rect(10,10,200,50),"Points: " + curPoints);  
    123.     GUI.Label(Rect(10,30,200,50),"Life: " + Life);    
    124. }
    125. @script ExecuteInEditMode()