Search Unity

Space Shooter Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Mar 26, 2015.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The transform.position of the prefab asset should be overwritten by the Instantiate () function. It is best to have the transform.position of the prefab asset at origin for OCD purposes, but not necessary.

    It is key, however, when creating the prefabs, that all the items in the prefab family are properly aligned with their transforms. For example, when creating a parent GameObject that has child artwork, make sure that the child is positioned correctly relative to that parent.
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I've been off-site doing a summit for that last week or so - so thanks for your patience.

    Special thanks to @OboShape, @unidad3d, @Map_Monkey and everyone else who helped answer questions!
     
    Map_Monkey likes this.
  3. XxBeselXx

    XxBeselXx

    Joined:
    Jan 19, 2016
    Posts:
    10
    Well, let me try to be simple:
    Code (CSharp):
    1. IsFinite(outDistanceAlongView) ; IsFinite(outDistanceForSort) ; Invalid AABB a ; aabb.IsFinite()
    2.  
    3. .
    These are the error messages wich comes whatever if i collide the asteroid with the player or with the bolt. The result is a very annoying lag on the game.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This seems to be an issue that was introduced in 5.3 or 5.3.1.

    I have confirmed that this issue has been solved in 5.3.1.p3 (and .p4)

    Unless you absolutely need to, I would suggest using 5.3.1 until the proper release of 5.3.2 is available, which will contain the fix. I've been told that 5.3.2 is "imminent", so it shouldn't be a long wait.

    If you want, try using the patch release to finish this tutorial error-free, and then use the main release for your production projects unless you find you are getting this issue in your production.

    https://unity3d.com/unity/qa/patch-releases
     
    OboShape likes this.
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    See the post above.

    This is an issue with 5.3.1.

    Try using the patch release to finish this project error free, but I would not recommend using a patch release for production unless you have this error in your production project. This is because patch releases have not been tested as thoroughly as a proper release would be.
     
  6. XxBeselXx

    XxBeselXx

    Joined:
    Jan 19, 2016
    Posts:
    10
    Well, thanks for the answer. For now, i'm still learning how to deal with the unity engine, so it wont be a problem, but thanks for the advice
     
  7. Brunocrb

    Brunocrb

    Joined:
    Aug 4, 2015
    Posts:
    4
    Hi,

    I've already finished the tutorial and I was trying to set up some different things in the game.
    By now, I was trying to make asteroids to move in zig zag .
    Then I modified the Evasive Manouver script and I got it working pretty well.
    The asteroid has a Mover script and this new one that I modified.
    If I throw it on the hierarchy, it works.
    However when I try to put them on the GameController, these asteroids don't fall.
    They stay going left and right but they really don't change the Z.

    I attached the script I created for the zig zag movement here, so you can see.
    Can you help me with this?
    Thanks!

    ---------------------------------------------------------------------------------------------------------
    The Code:
    ---------------------------------------------------------------------------------------------------------

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoverTestes4 : MonoBehaviour {
    5.    
    6.     public float dodge;
    7.     public float smoothing;
    8.     public float tilt;
    9.     public Boundary boundary;
    10.    
    11.     private float currentSpeed;
    12.     private float targetManeuver;
    13.     private Rigidbody rb;
    14.    
    15.         void Start()
    16.         {
    17.             rb = GetComponent<Rigidbody>();
    18.             currentSpeed = rb.velocity.z;
    19.             StartCoroutine(Evade());
    20.         }
    21.    
    22.         IEnumerator Evade()
    23.         {
    24.             yield return new WaitForSeconds(0);
    25.             while (true)
    26.             {
    27.                 targetManeuver = dodge * -Mathf.Sign(transform.position.x);
    28.                 yield return new WaitForSeconds(1);
    29.             }
    30.         }
    31.        
    32.         void FixedUpdate()
    33.         {
    34.             float newManeuver = Mathf.MoveTowards(rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    35.             rb.velocity = new Vector3(newManeuver, 0.0f, currentSpeed); //Motivo do problema de não descer.
    36.             rb.position = new Vector3
    37.             (
    38.                 Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    39.                 0.0f,
    40.                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    41.             );
    42.             rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
    43.         }
    44.  
    45. }
    46.  
     

    Attached Files:

    Last edited: Jan 31, 2016
  8. jenskop

    jenskop

    Joined:
    Jan 29, 2016
    Posts:
    4
    I don't get it. Is there still something I'm doing wrong? Can anybody help me?

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    public float speed;

    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }

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

    rb.movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.velocity = movement * speed;
    }
    }
     
  9. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    nah, thanks to you for great tutorial and support.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags when posting code on the forums.
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags when posting code on the forums.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please state what the problem is?
     
  13. Hubbard_Stephen

    Hubbard_Stephen

    Joined:
    Jan 29, 2016
    Posts:
    3
    Edit: Nvm I figured it out.

    Hey y'all, first post.

    I'm done with the Space Shooter tutorial, and now trying to add some of my own things. Huge programmer noobie here. I'm trying to make a GUI text holder to keep track of how many asteroids and/or enemy's have passed by me. And eventually lose the game when a certain amount gets through. I feel like I'm making this a lot more complicated than it has to be, but I still can't get it to work.

    How would I go about doing this?

    Currently I have this script and have it as an attached component to my Boundary.

    using UnityEngine;
    using System.Collections;

    public class ObjectsPassed : MonoBehaviour {


    private int p_Totalpassed;

    public GUIText ObjectsPassedText;

    void Start ()
    {
    p_Totalpassed = 0;
    ObjectsPassedText.text = "Objects Passed:";

    }

    void OnTriggerExit(Collider other)
    {
    if (other.CompareTag("Enemy"))
    {
    p_Totalpassed = p_Totalpassed + 1;

    }
    }

    void AddUnitsPassed(int newPassedValue)
    {
    p_Totalpassed += newPassedValue;
    UpdatePassedTotal();
    }

    void UpdatePassedTotal()
    {
    ObjectsPassedText.text = "Objects Passed: " + p_Totalpassed;
    }

    }


    ----

    I feel like I need to added the function 'AddUnitsPassed' somewhere to execute it properly. But I'm not sure how.

    Thanks
     
    Last edited: Jan 29, 2016
  14. jenskop

    jenskop

    Joined:
    Jan 29, 2016
    Posts:
    4
    It's supposed to move my spaceship but it says there are compiler errors. Here's what it says:
    59.PNG I'm just a noob I know.
     
  15. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning ,
    have a look at your line
    Code (CSharp):
    1. rb.movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    this line should be changed to declare and set the movement variable as a Vector3 type instead.
    Code (CSharp):
    1. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    then this movement Vector3 can then be used to set the velocity of the Rigidbody (rb).

    The error you are getting is telling you that the Rigidbody object or class doesnt have a method/property called movement which you have tried to access.

    but on the next line the Rigidbody does have a velocity property you can set, which is where your Vector3 comes in and allows the spaceship to dart about quite happily :)
     
  16. jenskop

    jenskop

    Joined:
    Jan 29, 2016
    Posts:
    4
    This fixed it hurray! I don't know why I used rb I think I saw it somewhere on this thread and used it. Thanks I can finally move on :).
     
  17. Map_Monkey

    Map_Monkey

    Joined:
    Jan 12, 2016
    Posts:
    8
    Enemy Ship Spawns with the expected x,y,z and never moves in the z again.

    There is a lot of "I think I know where to look. Here is what I found." The only place where I can synch with you again for playtesting with your video is after you complete the EvasiveManeuver.cs, and that covers a LOT of turf! :p

    In both the video and Done_Scene; Enemy Ship (Clone) instantiates and moves down the screen which matches the code progress until the Evasive Maneuver section. Everything works as intended. :)

    Once EvasiveManeuver.cs is attached and filled out is where there is a disconnect. Similar to the post above.

    Enemy Ship Spawns with the expected x,y,z and never moves in the z again.

    From that point on I start the usual debug routine

    I code a check to insure _I_ set a Rigidbody in Unity and Console a "Fix Me!" if things go bad.
    Passed? Say So in Console.

    I know I have assigned a Rigidbody to the prefab now. So what is the object's velocity on the z? Zero?

    You sure? Access that value through different ways and see if the video code, my code, and the Done_ code all get the same result? And they do. All zero = 0.0

    Which makes sense. Just instantiated object. I would expect zeroes or values from the Inspector. Double check values in the Inspector inside Unity. They LOOK the same to me other than the change Adam mistakenly made to z when he moved the Enemy Ship and then set z to 9 instead of the original 8, but that is merely a local change offset by the parent's origin location and can be ignored or changed to get the same results of no Z movement once EvasiveManeuver.cs is activated.

    Of the non-GUI functions the video uses; the pattern is Awake, Start, FixedUpdate, Update. In FixedUpdate this now all-important velocity in z of the Rigidbody is used to move it along the z. The code stores the value in currentSpeed which is initialized in Start. Which is zero no matter how I look at it.

    The only way I can get movement along the z is by fudging the magic number I know from Mover.cs and hard coding it if my tests fail. This is NOT the way to code. Especially when Adam says it inherits the velocity.

    So somewhere either I messed up something I overlooked or there is a hole in my logic I do not know about that does not involve magic numbers.

    Otherwise, I would not end up with this:
     

    Attached Files:

    Last edited: Jan 30, 2016
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use Code Tags when posting code on the forums.
     
  19. Brunocrb

    Brunocrb

    Joined:
    Aug 4, 2015
    Posts:
    4
    Fixed! Can anyone help me?
     
  20. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sorry - I may be just slow after a weekend doing the Global Game Jam... but...

    What do you need help with? Especially if it's fixed?

    Can you start again? Post what the entire problem is and if you have code, use code tags?
     
  21. Brunocrb

    Brunocrb

    Joined:
    Aug 4, 2015
    Posts:
    4
    Hi,

    How are you? I told "fixed" about the code tags, sorry for the misunderstanding!

    I've already finished the tutorial and I was trying to set up some different things in the game.
    By now, I was trying to make asteroids to move in zig zag .
    Then I modified the Evasive Manouver script and I got it working pretty well.
    The asteroid has a Mover script and this new one that I modified.
    If I throw it on the hierarchy, it works.
    However when I try to put them on the GameController, these asteroids don't fall.
    They stay going left and right but they really don't change the Z.

    I attached the script I created for the zig zag movement here, so you can see.
    Can you help me with this?
    Thanks!


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MoverTestes4 : MonoBehaviour {
    4.  
    5.     public float dodge;
    6.     public float smoothing;
    7.     public float tilt;
    8.     public Boundary boundary;
    9.  
    10.     private float currentSpeed;
    11.     private float targetManeuver;
    12.     private Rigidbody rb;
    13.  
    14.         void Start()
    15.         {
    16.             rb = GetComponent<Rigidbody>();
    17.             currentSpeed = rb.velocity.z;
    18.             StartCoroutine(Evade());
    19.         }
    20.  
    21.         IEnumerator Evade()
    22.         {
    23.             yield return new WaitForSeconds(0);
    24.             while (true)
    25.             {
    26.                 targetManeuver = dodge * -Mathf.Sign(transform.position.x);
    27.                 yield return new WaitForSeconds(1);
    28.             }
    29.         }
    30.      
    31.         void FixedUpdate()
    32.         {
    33.             float newManeuver = Mathf.MoveTowards(rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    34.             rb.velocity = new Vector3(newManeuver, 0.0f, currentSpeed); //Motivo do problema de não descer.
    35.             rb.position = new Vector3
    36.             (
    37.                 Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    38.                 0.0f,
    39.                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    40.             );
    41.             rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
    42.         }
    43. }
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sorry for the misunderstanding. I can spend a lot of time reading a lot of posts on the forum, and sometimes I forget where we are in all of these conversations.

    I'll look into this.
     
  23. Brunocrb

    Brunocrb

    Joined:
    Aug 4, 2015
    Posts:
    4
    That's ok, haha! Thanks!
     
  24. Map_Monkey

    Map_Monkey

    Joined:
    Jan 12, 2016
    Posts:
    8
    Can Scooby Doo and the gang solve "The Mystery of the Phantom Velocity?"
    Tune in next time.​

    I'll even give you a two for one deal! :D
     
  25. Mryogi23

    Mryogi23

    Joined:
    Feb 2, 2016
    Posts:
    7
    So I'm doing the space shooter tutorial in my game design class and something's going wrong on the part where im creating hazards. I did everything to the letter, but the bolt doesn't make the hazard vanish. Instead, it's merely pushing the asteroid away. What did I do wrong?
     
  26. TiredOwl

    TiredOwl

    Joined:
    Feb 2, 2016
    Posts:
    4
    HI, I'm on the 'Counting points and displaying the score' lesson and I've got a problem where objects are no longer being destroyed (the sound effects and explosions still work normally). I've tried checking for errors/differences in my scripts compared to the ones in the lesson with no luck. I'm not getting an error in the console either. Below are my 'DestroyByContact' and 'GameControllerScripts'. Please can you help!

    Code (CSharp):
    1.  
    2. public class DestroyByContact : MonoBehaviour
    3. {
    4.     public GameObject explosion;
    5.     public GameObject playerExplosion;
    6.     public int scoreValue;
    7.     private GameController gameController;
    8.  
    9.     void start ()
    10.     {
    11.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    12.         if (gameControllerObject != null)
    13.         {
    14.             gameController = gameControllerObject.GetComponent <GameController>();
    15.         }
    16.         if (gameController == null)
    17.         {
    18.             Debug.Log ("Cannot find 'GameController' script");
    19.         }
    20.     }
    21.  
    22.     void OnTriggerEnter (Collider other)
    23.     {
    24.         if (other.tag == "Boundary")
    25.         {
    26.             return;
    27.         }
    28.         Instantiate (explosion, transform.position, transform.rotation);
    29.         if (other.tag == "Player") {
    30.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    31.         }
    32.  
    33.         gameController.AddScore (scoreValue);
    34.         Destroy (other.gameObject);
    35.         Destroy (gameObject);
    36.  
    37.     }
    38. }
    39.  
    Code (CSharp):
    1.  
    2. public class GameController : MonoBehaviour
    3. {
    4.     public GameObject hazard;
    5.     public Vector3 spawnValues;
    6.     public int hazardCount;
    7.     public float spawnWait;
    8.     public float startWait;
    9.     public float waveWait;
    10.  
    11.     public GUIText scoreText;
    12.     private int score;
    13.  
    14.     void Start ()
    15.     {
    16.         score = 0;
    17.         UpdateScore ();
    18.         StartCoroutine (SpawnWaves ());
    19.     }
    20.     IEnumerator SpawnWaves ()
    21.     {
    22.         yield return new WaitForSeconds (startWait);
    23.         while (true)
    24.         {  
    25.             for (int i = 0; i < hazardCount; i++)
    26.             {
    27.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    28.                 Quaternion spawnRotation = Quaternion.identity;
    29.                 Instantiate (hazard, spawnPosition, spawnRotation);
    30.                 yield return new WaitForSeconds (spawnWait);
    31.             }
    32.             yield return new WaitForSeconds (waveWait);
    33.         }
    34.     }
    35.  
    36.     public void AddScore (int newScoreValue)
    37.     {
    38.         score += newScoreValue;
    39.         UpdateScore ();
    40.     }
    41.  
    42.     void UpdateScore ()
    43.     {
    44.         scoreText.text = "Score: " + score;
    45.     }
    46.  
    47.  
    48. }
    49.  
     
  27. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    Look for the "Is trigger" on the colliders.
     
  28. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30

    Probably the problem is here:

    Code (CSharp):
    1.     void start ()
    2.  
    3. should be
    4.  
    5.     void Start ()
     
  29. baisida

    baisida

    Joined:
    Feb 2, 2016
    Posts:
    1
    in the "boundary" lesson, when I add the DestroyByBoundary script to the game object Boundary, my Player just disappear, do I make something wrong? how could the script just destroy the shots not the Player? Thank you!
     
  30. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    With Tags, watch the full video!!
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is probably because you do not have the Collider is set to "Is trigger". Don't forget these collisions are based on trigger colliders.
     
  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sorry to say I don't see anything obvious that would prevent your hazards from being destroyed. However, I can see that in the first script, that you have not spelt start correctly. Don't forget that C Sharp is case-sensitive, and Start needs to begin with a capital S.

    Can you give us any more information?
     
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks @unidad3d!! I hadn't seen that you'd answered these, sorry for the duplicate posts. Your answers were fine.
     
  34. Mryogi23

    Mryogi23

    Joined:
    Feb 2, 2016
    Posts:
    7
    I know this may sound minor, but when I watch the audio lesson, he tells me to deselect the 3D Sound. But the thing is, the version of unity I'm using doesn't have that box. So do I ignore it or remove the audio in another way?
     
  35. TiredOwl

    TiredOwl

    Joined:
    Feb 2, 2016
    Posts:
    4
    Thank you sooo much to you and Adam! That one little letter fixed everything, can't believe I didn't spot it. Was quite fun having an invincible ship for a while though :)
     
  36. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Thanks Adam. it works with alt gr. Sorry for posting this question on this forum.
     
  37. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    Crikey, I'm lost regarding the obsolete rigidbody references which need to be replaced in Unity 5. :( I'm stuck at knowing what my script should look like during video 5 'Moving the Player'.

    The upgrade help isn't giving me much help as I don't yet fully understand a lot of the terms being used and it mentions things like 'The line being written in the video should now read as:' when I don't know what line it's refering to?

    Could someone paste what the PlayerController script 'should' look like (at the end of video 5) when written for Unity 5, so that I can work out the correct usage?
     
  38. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
  39. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    Thanks for the reply unidad3d. Unfortunately that code is for older versions of Unity and doesn't work on 5. :(

    I'd like to see the completed PlayerController code for Unity 5. I'd really appreciate someone positing it.
     
  40. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    So just to elaborate, by following the Unity5 help, my code at timecode 6:40 just before playmode is entered is the following.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb = GetComponent<Rigidbody>();
    11.  
    12.         float moveHorizontal = Input.GetAxis ("Horizontal");
    13.         float moveVertical = Input.GetAxis ("Vertical");
    14.  
    15.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    16.         rb.velocity = movement;
    17.     }
    18. }
    However when I press play my ship doesn't move. Could someone please point out why my code isn't working? :(

    On line 16, I've also tried the following code mentioned in the Unity5 help, which results in a red error.
    Code (CSharp):
    1. rb.velocity = some Vector3 value;
     
    Last edited: Feb 6, 2016
  41. unidad3d

    unidad3d

    Joined:
    Jan 10, 2016
    Posts:
    30
    Sorry, I tought they updated that code.

    The problem could be here:

    Code (CSharp):
    1. 10.        rb = GetComponent<Rigidbody>();
    2.  
    3. should be
    4.  
    5. 10.        rb = GetComponent <Rigidbody>();
     
  42. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    Thanks again for the reply unidad3d. Unfortunately I'm still getting the following error:
    All compiler errors have to be fixed before you can enter playmode!
    UnityEditor.SceneView:ShowCompileErrorNotification()

    This is my code currently.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb = GetComponent <Rigidbody>();
    11.    
    12.         float moveHorizontal = Input.GetAxis ("Horizontal");
    13.         float moveVertical = Input.GetAxis ("Vertical");
    14.  
    15.         rb.velocity = new Vector3(moveHorizontal, 0.0f, moveVertical);
    16.         rb.velocity = some Vector3 value;
    17.     }
    18. }
     
  43. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning @gizmo1990,

    I'll start off by saying the primary difference, which you have already got here is how we access the Rigidbody of our objects.

    you have declared your rb variable to hold a Rigidbody type, thats fine.
    and in your Start() function you have given your rb variable a value, which is fine.
    however, the Start function should end there.
    Code (CSharp):
    1.   private Rigidbody rb;
    2.  
    3.     void Start ()
    4.     {
    5.         rb = GetComponent<Rigidbody>();
    6.    }
    since all the code here is within the Start function, it is only ran once, thereby only checking your input once.

    we need to have a way of constantly checking for the player input throughout the game, and since we will be changing the physics characteristics of our player, we need to use FixedUpdate() so all of your input checks should be in this function instead.

    what I would maybe advise, is to have another watch through of the Moving the player video to see how Adam explains it.

    http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147

    [edit]
    just noticed the last post, yep, this will indicate that there are errors, however, you need to click on the console window to see what these errors are, and this should point you in the right direction
     
  44. EmperorDPengin

    EmperorDPengin

    Joined:
    Feb 7, 2016
    Posts:
    3
    i have tried everything to make the ship move and apparently there are a lot of people with the same problem, and after reading every single post i still cant get it to work, can someone look at the code and tell me if i am missing something? thank you ( it does recognize when i press the fire button so i dont think it is a input issue


    Code (CSharp):
    1. [System.Serializable]
    2. public class Boundary
    3. {
    4.     //variables to keep the player within the screen
    5.     public float xMin,xMax, zMin, zMax;
    6. }
    7.  
    8. public class PlayerControl : MonoBehaviour {
    9.  
    10.     private Rigidbody rb;
    11.     public float speed;
    12.     public float tilt;
    13.     public Boundary boundary;
    14.  
    15.     public GameObject Shot;
    16.     public Transform ShotSpawn;
    17.     public float fireRate;
    18.     private float nextFire;
    19.  
    20.  
    21.  
    22.     // Use this for initialization
    23.     void Start ()
    24.     {
    25.  
    26.         rb = GetComponent <Rigidbody>();
    27.  
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.  
    33.         if ( Input.GetButton("Fire1") && Time.time > nextFire)
    34.         {
    35.             nextFire = Time.time + fireRate;
    36.         Instantiate (Shot, ShotSpawn.position, ShotSpawn.rotation);
    37.         }
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void FixUpdate ()
    42.     {
    43.         //make the player move with inputs from LEFT and RIGHT keys.
    44.         float moveHorizontal = Input.GetAxis ("Horizontal");
    45.         float moveVertical = Input.GetAxis ("Vertical");
    46.  
    47.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    48.         rb.velocity = movement * speed;
    49.  
    50.         // use mathf.clamp to CLAMP a player to a soecific x,y,z min and max.
    51.         rb.position = new Vector3 (
    52.             Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    53.             0.0f,
    54.             Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    55.         );
    56.  
    57.         //quaternion.euler used to rotation (x,y,z)* -tilt or the ship will turn the opposite way.
    58.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    59.  
    60.     }
     
  45. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,

    looking at your code you have miss-spelld FixedUpdate()

    change
    Code (CSharp):
    1. void FixUpdate ()
    to
    Code (CSharp):
    1. void FixedUpdate ()
    This MonoBohaviour function wont throw any errors, it simply wont work. so anything within its body wont be called or checked if spelled incorrectly, or in the incorrect caSe sensitivity.
     
  46. EmperorDPengin

    EmperorDPengin

    Joined:
    Feb 7, 2016
    Posts:
    3
    thank you so very much ^^ i am stupid to not notice that
     
  47. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    No not at all, dont say that, its a learning curve mate, once bitten twice shy ;)
     
  48. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    Thanks for the help OboShape, I fixed it and got the ship moving! :)
    I've followed through with the Moving the player video and understood it all, however my code isn't working and throws up 3 errors?

    Assets/Scripts/PlayerController.cs(17,12): error CS1519: Unexpected symbol `void' in class, struct, or interface member declaration
    Assets/Scripts/PlayerController.cs(31,37): error CS1526: A new expression requires () or [] after type
    Assets/Scripts/PlayerController.cs(32,17): error CS1525: Unexpected symbol `)', expecting `;'

    I can't see any reason why those lines of code are wrong? Can anyone spot what I'm doing wrong? Also, my Mathf type does not seem to be recognised by monodevelop, as in it doesn't turn blue? Why would this be??

    [edit]
    Found it! :) I didn't have a comma after the 0.0f on line 30!! Phew!

    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.     private Rigidbody rb;
    13.     public float speed;
    14.     public float tilt;
    15.     public Boundary boundary
    16.  
    17.     void FixedUpdate ()
    18.     {
    19.         rb = GetComponent <Rigidbody>();
    20.  
    21.         float moveHorizontal = Input.GetAxis ("Horizontal");
    22.         float moveVertical = Input.GetAxis ("Vertical");
    23.  
    24.          Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    25.         rb.velocity = movement * speed;
    26.  
    27.         rb.position = new Vector3
    28.         (
    29.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    30.                 0.0f
    31.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    32.         );
    33.  
    34.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    35.     }
    36. }
     
    Last edited: Feb 7, 2016
  49. Map_Monkey

    Map_Monkey

    Joined:
    Jan 12, 2016
    Posts:
    8
    I would have gone with:
    Code (csharp):
    1. public Boundary boundary;  // Error Ln 17. Check Line. Trace back. Missed ; in Ln 15
    The other error has already been repaired. :)
     
  50. Map_Monkey

    Map_Monkey

    Joined:
    Jan 12, 2016
    Posts:
    8
    Am I correct in concluding that any Instantiated "clone" has it's Rigidbody.velocity overwritten as well? Or am I accessing the data incorrectly?