Search Unity

scripting for space shooter

Discussion in 'Scripting' started by Synder, Sep 5, 2015.

  1. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    I am trying to do the space shooter tutorial and the first scripted code that you have to write isn't correct in the tutorial since it is in a later version so there is the updated code in the space below. but when I input it into monodev as instructed it has issues. here is the code:

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }

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

    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.position = new Vector3
    (
    Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );

    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
    }

    can anyone explain what I am supposed to be fixing?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    hi, please use [code ][/code ] tags when pasting code into the forums, helps with the readability. There is a sticky on them at the top of the scripting section.

    In unity 5 they removed the default quick accessors like "rigidbody", you now need to explicitly create them.

    Basically add

    Code (csharp):
    1.  
    2. // class declaration of variable
    3. Rigidbody rigidbody;
    4.  
    5. void Start()
    6. {
    7. // initialize variable
    8. rigidbody = GetComponent<Rigidbody>();
    9. }
    10.  
     
  3. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15


    okay so that cleaned some of it up but now these two alerts are popping up in the unity console

    -Assets/Scripts/PlayerController.cs(15,19): warning CS0108: `PlayerController.rigidbody' hides inherited member `UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended

    -Assets/Scripts/PlayerController.cs(15,19): warning CS0414: The private field `PlayerController.rigidbody' is assigned but its value is never used

    here is the updated code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Boundary boundary;
    15.     Rigidbody rigidbody;
    16.  
    17.     void Start()
    18.     {
    19.         rigidbody = GetComponent<Rigidbody> ();
    20.     }
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         float moveHorizontal = Input.GetAxis ("Horizontal");
    25.         float moveVertical = Input.GetAxis ("Vertical");
    26.        
    27.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    28.         GetComponent<Rigidbody>().velocity = movement * speed;
    29.        
    30.         GetComponent<Rigidbody>().position = new Vector3
    31.             (
    32.                 Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    33.                 0.0f,
    34.                 Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    35.                 );
    36.        
    37.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    38.     }
    39. }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    once rigidbody is set in the Start function you don't need to (and shouldn't really for performance reasons) replace all the other rigidbody references... but anyway.


    Can you confirm which version of unity you are using?
     
  5. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    I am using version 5.1.3.f1
    I have replace rigidboy where it needs to be but now something is wrong with the velocity, position and rotation. Also how when in the game view mode in unity and play is pressed I could not figure out what keys to press in order to move the character object. If you know what the defaults are that would be awesome
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Boundary boundary;
    15.     Rigidbody rigidbody;
    16.  
    17.     void Start()
    18.     {
    19.         rigidbody = GetComponent<Rigidbody> ();
    20.     }
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         float rigidbody;
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.        
    28.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    29.         rigidbody().velocity = movement * speed;
    30.        
    31.         rigidbody().position = new Vector3
    32.             (
    33.                 Mathf.Clamp (rigidbody().position.x, boundary.xMin, boundary.xMax),
    34.                 0.0f,
    35.                 Mathf.Clamp (rigidbody().position.z, boundary.zMin, boundary.zMax)
    36.                 );
    37.        
    38.         rigidbody().rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody().velocity.x * -tilt);
    39.     }
    40. }
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    I got it to work! Thank you so much!

    I am still new to all this but am loving it so far.
     
  8. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    I don't know if you are still getting this warning

    -Assets/Scripts/PlayerController.cs(15,19): warning CS0108: `PlayerController.rigidbody' hides inherited member `UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended

    if so you should replace

    Code (csharp):
    1. Rigidbody rigidbody;
    with

    Code (csharp):
    1. new Rigidbody rigidbody;
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    except you don't use "new" when dealing with components, you let the unityengine create them so they're handled correctly.
     
  10. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    Yes and no. In case of instantiating you don't use new. However in the case of telling the compiler that the Rigidbody field you have added to your class is hiding the field named Rigidbody on UnityEngine.Component it is perfectly alright to do because it has nothing to do with instantiating.
     
  11. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    Okay so I had gotten everything working and yes that took away the warning. Now I am trying to get the asteroid to tumble. the only problem is I am using the given code and I have tried code in their API but so far the only movement I've gotten is directional instead of rotational.
    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomRotator : MonoBehaviour {
    5.  
    6.     public float tumble;
    7.     new Rigidbody rigidbody;
    8.  
    9.     void Start ()
    10.     {
    11.         rigidbody.angularVelocity= Random.insideUnitSphere * tumble;
    12.    
    13.     }
    14. }
     
  12. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    Is it plain and simple constant rotation you want?
     
  13. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    for the moment yes. later i will add in the movement

    I already have my destroy by contact code in place and i believe it is working. I just need it to rotate and since there is a glitch in the code it wont let me test in the play mode.

    this is the error i have been getting

    NullReferenceException: Object reference not set to an instance of an object
    RandomRotator.Start () (at Assets/Scripts/RandomRotator.cs:11)
     
  14. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    I'm thinking you should use AddTorque instead of angularVelocity. Setting the angular velocity manually will overwrite the rotational effect of one astroid colliding with another. In therory you should only need to call AddTorque once if you have set angularDrag to zero.
     
  15. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    Oh sorry I just now noticed you are setting the value in your Start method. Be right back.
     
  16. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    I just tested your code.

    1) Did you assign the rigidbody of your script? I cannot see it in your code and it does not show up in the editor
    2) Did you uncheck "Use Gravity" on the astroid's rigidbody component?
     
  17. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    Yes gravity is turned off.

    Assign rigidbody how so?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomRotator : MonoBehaviour {
    5.  
    6.     public float tumble;
    7.     new Rigidbody rigidbody;
    8.    
    9.     void Start ()
    10.     {
    11.         rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
    12.     }
    13. }
    14.  
     
  18. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    You can assign the value of your rigidbody field in multiple ways.

    If the RandomRotator script is attached to the same game object that has the Rigidbody you can do this in the Start method

    this.rigidbody = this.GetComponent<Rigidbody>();

    You can also choose to make the rigidbody field public and then drag'n'drop the rigidbody into the field in the editor.
     
  19. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    That first one worked. Awesome thanks. So I need to remember to GetComponent<Rigidbody>(); whenever calling that in the code.

    I'm sure I'll have more question in the near future being I am aiming to finish this game today.
     
    Last edited: Sep 6, 2015
  20. ReadySetJoe

    ReadySetJoe

    Joined:
    Feb 10, 2016
    Posts:
    2
    Hello all!

    I am brand new to Unity (and C#) having only used it for a few weeks now, and am utilizing the tutorials.
    I've been following the thread because I had issues with Unity 5 handling scripting differently than in the tutorial, and have made the changes brought up in this discussion (thanks for the help).

    Before I ask a question, I'll give some context:
    I was previously following the Upgrade Guide for Unity 5 for this tutorial up until the scripting part (page 14) where it says to create a reference variable "rb" for the rigidbody.

    Out of curiosity, why did you do things differently to the Guide and how did you know to do what you did?
    Additionally, I'd like to understand how to use reference variables as used in the Upgrade Guide.
    (Again, brand new to Unity and C#)

    Thanks!