Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Survival Shooter Q&A

Discussion in 'Community Learning & Teaching' started by willgoldstone, Jul 3, 2015.

  1. travis272

    travis272

    Joined:
    Aug 17, 2015
    Posts:
    3
    Hi have an issue with the 1st video I am getting an error of:
    Assets/Scripts/Player/PlayerMovement.cs(48,30): error CS1525: Unexpected symbol `Quaternion'

    I have read through the completed coding and have rewritten parts and try to copy segments directly and I cant solve this issue

    my coding is:

    1 using UnityEngine;
    2
    3 public class PlayerMovement : MonoBehaviour
    4 {
    5 public float speed = 6f;
    6
    7 Vector3 movment;
    8 Animator anim;
    9 Rigidbody playerRigidbody;
    10 int floorMask;
    11 float camRayLangth = 100f;
    12
    13 void Awake()
    14 {
    15 floorMask = LayerMask.GetMask ("Floor");
    16 anim = GetComponents <Animator> ();
    17 playerRigidbody = GetComponent <Rigidbody> ();
    18 }
    19
    20 void FixedUpdate()
    21 {
    22 float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
    23 float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
    24
    25 Move (h, v);
    26 Turning ();
    27 Animation (h, v);
    28 }
    29
    30 void Move (float h, float v)
    31 {
    32 movment.Set (h, 0f, v);
    33
    34 movment = movment.normalized * speed * Time.deltaTime;
    35
    36 playerRigidbody.MovePosition (transform.position + movement);
    37 }
    38
    39 void Turning()
    40 {
    41 Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    42
    43 if (Physics.Raycast (camRay, out floorHit, camRayLanght, FloorMask))
    44 {
    45 Vector3 playerToMouse = floorHit.point - transform.position;
    46 playerToMouse.y = 0f
    47
    48 Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);
    49 playerRigidbody.moveRotation (newRotation);
    50 }
    51 }
    52
    53 void Animation(float h, float v)
    54 {
    55 bool walking = h != 0f || v !=0f;
    56 anim.SetBool ("IsWalking", walking);
    57 }
    58 }

    I am using unity 5.1.2f1 thianks
     
  2. tkn57

    tkn57

    Joined:
    Aug 19, 2015
    Posts:
    2
    I've just completed the tutorial after completing the space shooter and ball game tutorials. Everything works perfectly, thank you very much for creating such detailed tutorials and awesome assets.

    The only thing I am uncertain off is how the animations were created for the player and enemies. Can someone please point me to some tutorials on this topic.

    Thanks.
     
  3. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    The animations were likely created with another program like Maya, Blender etc.
     
  4. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    Line 46 looks like it needs a semicolon( ; ) at the end of it.

    so Instead of
    Code (CSharp):
    1. playerToMouse.y = 0f
    It needs to be

    Code (CSharp):
    1. playerToMouse = 0f;
    Let me know how that works for you.
     
  5. LichiMan

    LichiMan

    Joined:
    Apr 23, 2013
    Posts:
    6
    I have exactly the same problem.
    I've just found that there's an Animation Event in the Die animation that when it finishes, it restart the level.
    But I don't know why it breaks the lighting and I need to restart Unity to get it all working again.
     
  6. Buk Lau

    Buk Lau

    Joined:
    Aug 19, 2015
    Posts:
    2
    Hey all I'm having some trouble if anyone could help.

    I have finished the tutorial and everything is working fine except for one little thing...
    My lighting at Game Over restart.
    When I start my game it looks great, it's dark and the lighting is exactly how it is supposed to be.
    Screen Shot 2015-08-20 at 4.21.36 am.png

    But then after it finishes the Game Over animation and restarts, all the lighting looks like it has been removed.

    Screen Shot 2015-08-20 at 4.21.55 am.png

    Please help!
    Thanks
     
  7. LichiMan

    LichiMan

    Joined:
    Apr 23, 2013
    Posts:
    6
    Hi, I've just read the solution for the lighting "bug" by jaimemh in this thread. Here is it:
     
    qqoopp0 and Deluwen like this.
  8. Cornishmonkey

    Cornishmonkey

    Joined:
    Jul 22, 2015
    Posts:
    1
    Legend! I had this same typo. Couldn't see it for looking.
     
  9. adrianaaaa

    adrianaaaa

    Joined:
    Aug 7, 2015
    Posts:
    6
    I see in the _CompletedAssets folder scripts to make the game compatible with mobile devices. I swapped my scripts with those and indeed the Player is able to shoot on Android and iOS, but not to move. What am I missing? Thanks!

    PlayerMovement.cs
    --------------------------

    Code (CSharp):
    1. using UnityEngine;
    2. using UnitySampleAssets.CrossPlatformInput;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float speed = 6f;
    7.  
    8.     Vector3 movement;
    9.     Animator anim;
    10.     Rigidbody playerRigidbody;
    11. #if !MOBILE_INPUT
    12.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    13.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    14. #endif
    15.  
    16.         void Awake ()
    17.         {
    18. #if !MOBILE_INPUT
    19.             // Create a layer mask for the floor layer.
    20.             floorMask = LayerMask.GetMask ("Floor");
    21. #endif
    22.  
    23.         // Set up references.
    24.         anim = GetComponent<Animator> ();
    25.         playerRigidbody = GetComponent<Rigidbody> ();
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.         // Store the input axes.
    31.         float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
    32.         float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
    33.  
    34.         Move (h, v);
    35.         Turning ();
    36.         Animating (h, v);
    37.     }
    38.  
    39.     void Move (float h, float v)
    40.     {
    41.         movement.Set (h, 0f, v);
    42.         movement = movement.normalized * speed * Time.deltaTime;
    43.         playerRigidbody.MovePosition (transform.position + movement);
    44.     }
    45.  
    46.     void Turning ()
    47.     {
    48. #if !MOBILE_INPUT
    49.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    50.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    51.        
    52.         // Create a RaycastHit variable to store information about what was hit by the ray.
    53.         RaycastHit floorHit;
    54.        
    55.         // Perform the raycast and if it hits something on the floor layer...
    56.         if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    57.         {
    58.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    59.             Vector3 playerToMouse = floorHit.point - transform.position;
    60.            
    61.             // Ensure the vector is entirely along the floor plane.
    62.             playerToMouse.y = 0f;
    63.            
    64.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    65.             Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);
    66.            
    67.             // Set the player's rotation to this new rotation.
    68.             playerRigidbody.MoveRotation (newRotatation);
    69.         }
    70. #else
    71.        
    72.         Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Mouse X") , 0f , CrossPlatformInputManager.GetAxisRaw("Mouse Y"));
    73.        
    74.         if (turnDir != Vector3.zero)
    75.         {
    76.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    77.             Vector3 playerToMouse = (transform.position + turnDir) - transform.position;
    78.            
    79.             // Ensure the vector is entirely along the floor plane.
    80.             playerToMouse.y = 0f;
    81.            
    82.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    83.             Quaternion newRotatation = Quaternion.LookRotation(playerToMouse);
    84.            
    85.             // Set the player's rotation to this new rotation.
    86.             playerRigidbody.MoveRotation(newRotatation);
    87.         }
    88. #endif
    89.     }
    90.  
    91.  
    92.     void Animating(float h, float v)
    93.     {
    94.         bool walking = h !=0f || v != 0f;
    95.         anim.SetBool ("IsWalking", walking);
    96.     }
    97. }
    98.  
     
  10. yobtar

    yobtar

    Joined:
    Aug 12, 2015
    Posts:
    7
    Can someone help?. I added a new custom Model and spawnpoint. It spawns properly walks a attacks and dies, but when it does it doesnt score or sink to the floor and delete. I have attached the Enemy health Script that is working properly for all the standard assets. I am not sure what is wrong and where to fix it.
    Any help would be awesome!


    ·
     
  11. CDubbz79

    CDubbz79

    Joined:
    Jan 9, 2015
    Posts:
    1
    Ok so I am stuck on lesson 2. Here is my script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     Vector3 movement;
    8.     Animator anim;
    9.     Rigidbody playerRidgidbody;
    10.     int floorMask;
    11.     float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask ("Floor");
    16.         anim = GetComponent <Animator> ();
    17.         playerRidgidbody = GetComponent<Rigidbody> ();
    18.     }
    19.     void FixedUpdate()
    20.     {
    21.         float h = Input.GetAxisRaw ("Horizontal");
    22.         float v = Input.GetAxisRaw ("Vertical");
    23.  
    24.         Move (h, v);
    25.         Turning ();
    26.         Animating (h, v);
    27.     }
    28.  
    29.     void Move (float h, float v)
    30.     {
    31.         movement.Set (h, 0f, v);
    32.         movement = movement.normalized * speed * Time.deltaTime;
    33.         playerRidgidbody.MovePosition (transform.position + EnemyMovement);
    34.     }
    35.  
    36.     void Turning()
    37.     {
    38.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePresent);
    39.         RaycastHit floorHit;
    40.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    41.         {
    42.             Vector3 playerToMouse = floorHit.point - Transform.position;
    43.             playerToMouse.y = 0f;
    44.  
    45.             Quaternion newRotation = Quaternion.LoofRoatation (playerToMouse);
    46.             playerRidgidbody.MoveRotation (newRotation);
    47.         }
    48.     }
    49.  
    50.         void Animating(float h, float v)
    51.     {
    52.             bool walking = h != 0f || v != 0f;
    53.             anim.SetBool ("IsWalking", walking);
    54.     }
    55. }
    56.  
    I followed it completely while writing the PlayerMovement script but I am getting these errors:

    Assets/Scripts/Player/PlayerMovement.cs(50,30): error CS1547: Keyword `void' cannot be used in this context
    Assets/Scripts/Player/PlayerMovement.cs(50,31): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='

    I am hoping someone with a better scripting knowledge to help me out as I am an artist by trade! Thanks!
     
  12. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    At line 33 change EnemyMovement to movement
    At line 38 change .mousePresent to .mousePosition
    At line 43 change Transform.position to transform.position
    At line 46 change Quaternion.LoofRoatation to Quaternion.LookRotation
    At line 49 add }

    Tell me how that works for you. And if you need further explaining, I am a beginner programmer so I can only help so much. But ask anyways :p

    Are you using MonoDevelop for the scripting?
     
  13. yobtar

    yobtar

    Joined:
    Aug 12, 2015
    Posts:
    7
    I figured out what I did wrong I didnt have the animation Death Trigger set :)
     
    IanSmellis likes this.
  14. olonge

    olonge

    Joined:
    Sep 22, 2014
    Posts:
    16
    Enemy HealthBar, and mobile deployment

    First of all, thanks for a great tutorial, I've just migrated over from UE4 and loving it. Already tried Stealth.

    1st question. I'd like to add a health bar above each of the enemies, can someone please point me in the right direction for this.
    The HUD does not seem like the right way, since it is sort of a global view.

    2nd, I'd also like to deploy this in (Android)mobile, or IOS. @adrianaaaa mentioned above, that there might be some pointers in the _completedAssets. I'll check it out, but any suggestions will be appreciated

    Edit:
    I've just noticed that there is a live training session on converting the spaceship shooter to mobile at http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/space-shooter-to-mobile.
    The concepts should be the same. I'll have a go at converting that first, then coming back to convert this,
     
    Last edited: Aug 28, 2015
  15. ethanicus

    ethanicus

    Joined:
    Aug 24, 2015
    Posts:
    39
    I can't figure out why, but my player won't look at the mouse.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     Vector3 movement;
    8.     Animator anim;
    9.     Rigidbody playerRigidbody;
    10.     int floorMask;
    11.     float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask ("Floor");
    16.         anim = GetComponent <Animator> ();
    17.         playerRigidbody = GetComponent <Rigidbody> ();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw ("Horizontal");
    23.         float v = Input.GetAxisRaw ("Vertical");
    24.  
    25.         Move (h, v);
    26.         Turning ();
    27.         Animating (h, v);
    28.     }
    29.  
    30.     void Move (float h, float v)
    31.     {
    32.         movement.Set (h, 0f, v);
    33.  
    34.         movement = movement.normalized * speed * Time.deltaTime;
    35.  
    36.         playerRigidbody.MovePosition (transform.position + movement);
    37.     }
    38.  
    39.     void Turning()
    40.     {
    41.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    42.  
    43.         RaycastHit floorHit;
    44.  
    45.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    46.         {
    47.             Vector3 playerToMouse = floorHit.point - transform.position;
    48.             playerToMouse.y = 0f;
    49.  
    50.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    51.             playerRigidbody.MoveRotation (newRotation);
    52.         }
    53.     }
    54.  
    55.     void Animating(float h, float v)
    56.     {
    57.         bool walking = h != 0f || v != 0f;
    58.         anim.SetBool ("IsWalking", walking);
    59.     }
    60. }
    61.  
    I followed the guide as best I could, and I've double-checked everything. Can I get some help here?
     
  16. qqoopp0

    qqoopp0

    Joined:
    Aug 17, 2015
    Posts:
    7
    I hope the post of the admin added this reply at the #1 post since this is everyone's bug found in the YouTube comments. Also, the YouTube video did not link to this post. I was directed a number of times to reach here.

    Thanks. The black screen issue is solved.
     
    LichiMan likes this.
  17. Adham_star

    Adham_star

    Joined:
    Aug 27, 2015
    Posts:
    2
    Hello guys, I really can't download the assets for that game, I can't see any download button on that page. Can you help me please?
     
  18. Buk Lau

    Buk Lau

    Joined:
    Aug 19, 2015
    Posts:
    2
    Thank you so much. Much appreciated.
     
    LichiMan likes this.
  19. mindraker

    mindraker

    Joined:
    Aug 7, 2015
    Posts:
    1
    Hi there
    Have you checked "Has Exit Time" on transition arrow?
    I'd got same problem with you and i solved by unchecked "Has Exit Time" option.
    sorry for my broken english :)


     
  20. benalter

    benalter

    Joined:
    Aug 25, 2015
    Posts:
    2
    Hey,
    Just finished part 7 of the tutorial. My problem is that when I am running I can't shoot and can only start shooting whilst standing still. If I keep shooting I can then run and shoot but can't start shooting when running. Also there is a small delay when I stop running before being able to shoot again. Any ideas what is wrong?
     
  21. Stanczyk

    Stanczyk

    Joined:
    Feb 1, 2014
    Posts:
    6
    I've done this tutorial before. This time around, for some reason, my Zombears (with the purple outlines) emit a purple glow (they are spawned with a point light.) I've never experienced this. Using Unity 5.1.2f1. Did I tick a box I shouldn't have somewhere? The point light isn't present in the prefab.
     
  22. serph1190

    serph1190

    Joined:
    Sep 2, 2015
    Posts:
    2
    I am having an issue. everything has worked perfectly up til the end of the "spawning enemies" video. Whenever I hit play I am able to shoot at enemies that spawn until the 10 second mark when the Hellephant is supposed to spawn. When the Hellephant SHOULD spawn, the game stops instead and I get an error message at the bottom that states "IndexOutOfRangeException: Array index is out of range." I have checked and double checked the code and the location of spawn poitns on x,y, and z axis etc etc even went so far as to copy and paste the code from the transcript below and I get this issue no matter what. Thank you in advance for helping me. shooter error.jpg
     
  23. eXe1992

    eXe1992

    Joined:
    Sep 3, 2015
    Posts:
    1
    What is this proble? upload_2015-9-3_15-2-11.png
    Thank you for your help
     
  24. eqfizzgig

    eqfizzgig

    Joined:
    Jul 22, 2015
    Posts:
    2
    I am having an issue with getting the player to follow the mouse. I've checked over my code multiple times and have found lots of posts online with ideas of why this isn't working none of which so far have applied to me. My debug log returns 'ray didn't hit' and my floor is set with a floor layer which is what I would assume would cause that problem.

    Thanks for looking it over and any ideas on how to fix it.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     Vector3 movement;
    8.     Animator anim;
    9.     Rigidbody playerRigidbody;
    10.     int floorMask;
    11.     float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask("floor");
    16.         anim = GetComponent<Animator>();
    17.         playerRigidbody = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw("Horizontal");
    23.         float v = Input.GetAxisRaw("Vertical");
    24.  
    25.         Move (h, v);
    26.  
    27.         Turning();
    28.  
    29.         Animating(h, v);
    30.     }
    31.     void Move(float h, float v)
    32.     {
    33.         movement.Set(h, 0f, v);
    34.         movement = movement.normalized * speed * Time.deltaTime;
    35.         playerRigidbody.MovePosition(transform.position + movement);
    36.  
    37.     }
    38.  
    39.     void Turning()
    40.     {
    41.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    42.         Debug.Log("camRay " + camRay);
    43.         RaycastHit floorHit;
    44.  
    45.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    46.         {
    47.             Vector3 playerToMouse = floorHit.point - transform.position;
    48.  
    49.             playerToMouse.y = 0f;
    50.             Debug.Log("playerToMouse "+playerToMouse);
    51.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    52.             Debug.Log("newRotation "+newRotation);
    53.             playerRigidbody.MoveRotation(newRotation);
    54.             Debug.Log("playerRigidbody "+playerRigidbody);
    55.         }
    56.         else
    57.             Debug.Log("ray didnt hit");
    58.     }
    59.     void Animating(float h, float v)
    60.     {
    61.         bool walking = h != 0f || v != 0f;
    62.  
    63.         anim.SetBool ("IsWalking", walking);
    64.     }
    65. }
    66.  
    Screenshot 2015-09-05 03.41.40.png
     
  25. genyale

    genyale

    Joined:
    Sep 6, 2015
    Posts:
    1
    I have the same turning issue of lesson 2, the only problem is the rotation of the mouse, I've been working on a laptop, maybe the touchpad is the problem, can someone help me to figure out.

    Here is my code:


    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLenght = 100f;

    void Awake()
    {
    floorMask = LayerMask.GetMask ("floor");
    anim = GetComponent<Animator> ();
    playerRigidbody = GetComponent<Rigidbody> ();

    }

    void FixedUpdate()
    {
    float h = Input.GetAxisRaw ("Horizontal");
    float v = Input.GetAxisRaw ("Vertical");

    Move (h, v);
    Turning ();
    Animating (h, v);
    }

    void Move (float h, float v)
    {
    movement.Set (h, 0f, v);

    movement = movement.normalized * speed * Time.deltaTime;

    playerRigidbody.MovePosition (transform.position + movement);

    }

    void Turning()
    {
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

    RaycastHit floorHit;

    if (Physics.Raycast (camRay, out floorHit, floorMask))
    {
    Vector3 playerToMouse = floorHit.point - transform.position;
    playerToMouse.y = 0f;

    Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    playerRigidbody.MoveRotation (newRotation);

    }

    }

    void Animating(float h, float v)
    {
    bool walking = h != 0f || v != 0f;
    anim.SetBool ("IsWalking", walking);

    }


    }
     
  26. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    Try to capitalize Floor on line 15

    Code (CSharp):
    1.         floorMask = LayerMask.GetMask("floor");
    should be
    Code (CSharp):
    1.         floorMask = LayerMask.GetMask("Floor");
    Tell me how that goes
     
  27. eqfizzgig

    eqfizzgig

    Joined:
    Jul 22, 2015
    Posts:
    2
    Perfect thanks. sometimes you just need a second set of eyes.
     
    IanSmellis likes this.
  28. Robotato500

    Robotato500

    Joined:
    Sep 7, 2015
    Posts:
    1
    Hi, I've got a problem.
    When I try to download the assets for Unity 5, Firefox say that I have to select an aplication to open the file. I select Unity Editor, and I click in O.K. But then, Unity starts normal, like when I start the program, and nothing happens. Please help me:(.
    My version is Unity 5.1.3f1 (64-bit).
     
  29. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    Try going to the asset store in Unity itself

    Click the "Window" drop down menu then click "Asset Store"

    Optionally you can just press Ctrl+9
     
  30. gytfunke

    gytfunke

    Joined:
    Sep 4, 2015
    Posts:
    2
    Hi, I've got an error I can't figure out which developed for me near the end of Lesson 2. I've been working on it for a while. Google searched it and while I've found other people with the same problem, have found no solution. Help? :) Thanks.

    Unity throws an error stating that my Player does not have a rigidbody, but the Player Movement script is trying to access it anyway, so... error.

    Here's the error:


    Here's a screenshot of my Player object with the rigidbody component attached:



    And my player movement script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     private Vector3 movement;
    8.     private Animator anim;
    9.     private Rigidbody rb;
    10.     private int floorMask;
    11.     private float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask ("Floor");
    16.         anim = GetComponent <Animator> ();
    17.         rb = GetComponent <Rigidbody> ();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw ("Horizontal");
    23.         float v = Input.GetAxisRaw ("Vertical");
    24.  
    25.         Move (h, v);
    26.         Turning ();
    27.         Animating (h, v);
    28.     }
    29.  
    30.     void Move (float h, float v)
    31.     {
    32.         movement.Set (h, 0f, v);
    33.         movement = movement.normalized * speed * Time.deltaTime;
    34.         rb.MovePosition (transform.position + movement);
    35.     }
    36.  
    37.     void Turning ()
    38.     {
    39.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    40.         RaycastHit floorHit;
    41.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    42.         {
    43.             Vector3 playerToMouse = floorHit.point - transform.position;
    44.             playerToMouse.y = 0f;
    45.  
    46.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    47.             rb.MoveRotation (newRotation);
    48.         }
    49.     }
    50.  
    51.     void Animating (float h, float v)
    52.  
    53.     {
    54.             bool walking = h != 0f || v != 0f;
    55.             anim.SetBool ("IsWalking", walking);
    56.     }
    57. }
    58.  
     
  31. gytfunke

    gytfunke

    Joined:
    Sep 4, 2015
    Posts:
    2
    Found it. Look at the Player object in the Hierarchy. There's another Player object attached to it. For some reason the PlayerMovement script got attached to that object as well, which of course didn't have a rigidbody. :oops: Removed the script from the child and voila, it works.
     
    Last edited: Sep 8, 2015
    deve2k and IanSmellis like this.
  32. DanRigg11

    DanRigg11

    Joined:
    Aug 26, 2015
    Posts:
    2
    Hi guys this is the first tutorial I have done with unity and I want to include levels... I think I'd no how to how to display the level number but what would I have to change in the enemy spawn script to create levels.
    Thanks
    Dan
     
    Last edited: Sep 9, 2015
  33. benalter

    benalter

    Joined:
    Aug 25, 2015
    Posts:
    2
    Hey,
    Just finished part 7 of the tutorial. My problem is that when I am running I can't shoot and can only start shooting whilst standing still. If I keep shooting I can then run and shoot but can't start shooting when running. Also there is a small delay when I stop running before being able to shoot again. Any ideas what is wrong?
     
  34. Wilave

    Wilave

    Joined:
    Sep 10, 2015
    Posts:
    3
    Just finished the second tutorial in Unity 5, and my character doesn't seem to move at all whatsoever. I have tried unchecking "has exit time" and I've combed over my code, and can't seem to find whats wrong. I can upload the project if someone could take a look.

    here is my code

    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 100f;

    void Awake()
    {
    floorMask = LayerMask.GetMask("Floor");
    anim = GetComponent<Animator>();
    playerRigidbody = GetComponent<Rigidbody>();
    }

    void Fixedupdate()
    {
    float h = Input.GetAxisRaw("Horizontal");
    float v = Input.GetAxisRaw("Vertical");

    Move(h, v);
    Turning();
    Animating(h, v);
    }

    void Move(float h, float v)
    {
    movement.Set(h, 0f, v);
    movement = movement.normalized * speed * Time.deltaTime;
    playerRigidbody.MovePosition(transform.position + movement);
    }

    void Turning()
    {
    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit floorHit;

    if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    {
    Vector3 playerToMouse = floorHit.point - transform.position;
    playerToMouse.y = 0f;

    Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    playerRigidbody.MoveRotation(newRotation);
    }
    }

    void Animating(float h, float v)
    {
    bool walking = h != 0f || v != 0f;
    anim.SetBool("IsWalking", walking);
    }

    public Rigidbody PlayerRigidbody { get; set; }
    }
     
  35. orchard800

    orchard800

    Joined:
    Jul 28, 2015
    Posts:
    19
    I've just written the PlayerMovement script. My player moves about and rotates with the mouse - but when I am holding a movement key down (say W) while the character moves the mouse position is frozen. When the character is not moving, the mouse position can be moved around. Is anyone else experiencing this?

    I've checked several times for errors in copying the code form the tutorial but it's just as it is written in the tutorial.

    Can someone help me please?
     
  36. jamesshaw1234

    jamesshaw1234

    Joined:
    Jul 1, 2015
    Posts:
    3
    I got to the end of lesson 2 and went to play the game to test it and the walking animation is not working? help! using unity 5.1.1 using the most recent assets package.

    here is my script:

    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 100f;

    void Awake()
    {
    floorMask = LayerMask.GetMask ("Floor");
    anim = GetComponent<Animator> ();
    playerRigidbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
    float h = Input.GetAxisRaw ("Horizontal");
    float v = Input.GetAxisRaw ("Vertical");

    Move (h, v);
    Turning ();
    Animating (h, v);
    }

    void Move(float h,float v)
    {
    movement.Set (h, 0f, v);

    movement = movement.normalized * speed * Time.deltaTime;

    playerRigidbody.MovePosition (transform.position + movement);
    }

    void Turning()
    {
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

    RaycastHit floorHit;

    if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    {
    Vector3 playerToMouse = floorHit.point - transform.position;
    playerToMouse.y = 0f;

    Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    playerRigidbody.MoveRotation(newRotation);
    }
    }

    void Animating(float h, float v)
    {
    bool walking = h != 0f || v != 0f;
    anim.SetBool ("IsWalking", walking);
    }
    }
     
  37. batpox

    batpox

    Joined:
    Sep 15, 2015
    Posts:
    6
    I think this problem, and also the one with turning (the "raycast length has to be eliminated" issue) are both related to the same issue: how the Input object works with the mouse on Windows (Note: I am using Windows 8.1 with Unity 5.1.2).

    When I add a debug log, the mouse GetRawAxis for "Horizontal" and "Vertical" return x and y values that are opposite what this game expects. Also, the Input.mousePosition (which is a Vector3 - instead of Vector2 - for some reason) has the screen pixel coordinates in the X and Y slots (Z is zero). If you use a 3 monitor development environment (as I do) and have the Unity3d IDE on the third monitor, this can be a rather large value.

    How to fix? There are several ways, but as I am new to this, I am exploring the Unity Input object to make sure the solution is consistent with the Unity3D developers' philosophy.

    Hope this helps a bit
     
  38. preslio95

    preslio95

    Joined:
    Sep 8, 2015
    Posts:
    4
    Hello!
    I need help please! I'm on the Moving Character Tutorial and I have a problem with the animation, or at least I think so. I've made the script exactly like the one in the video and I've followed every step twice, but still when I enter game mode, my character moves, but there isn't any movement animation, he moves like a ghost, just floating.
    I'm open to any suggestions.
     
  39. CodeGorilla

    CodeGorilla

    Joined:
    Sep 15, 2015
    Posts:
    17
    This is also happening to me, however if you keep moving the character after around 5-10 seconds the walking animation plays.

    I have tried to look for a fix for this, but I can't work it out! :(
     
  40. gstephin

    gstephin

    Joined:
    Sep 18, 2015
    Posts:
    1
    in my player movement script.. player is not turning..! what may be the problem ..?
     
  41. CodeGorilla

    CodeGorilla

    Joined:
    Sep 15, 2015
    Posts:
    17
    Post your script so we can take a look please.
     
  42. CodeGorilla

    CodeGorilla

    Joined:
    Sep 15, 2015
    Posts:
    17
    I have an issue with the respawn part.
    My player respawns when he dies and the Game Over text shows etc..
    However, when I change the value of the respawn timer it doesn't do anything, the respawn doesn't take longer.
    I want it longer than the set 5, but when I change it in the GameOverManager script it doesn't make it longer.

    This is my GameOverManager script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GameOverManager : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;
    6.     public float restartDelay = 5f;
    7.  
    8.     Animator anim;
    9.     float restartTimer;
    10.  
    11.  
    12.     void Awake()
    13.     {
    14.         anim = GetComponent<Animator>();
    15.     }
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         //is the player dead? - if yes then play the game over message
    21.         if (playerHealth.currentHealth <= 0)
    22.         {
    23.             anim.SetTrigger("GameOver");
    24.             restartTimer += Time.deltaTime;
    25.  
    26.             if(restartTimer >= restartDelay)
    27.             {
    28.                 Application.LoadLevel("Level01");
    29.             }
    30.         }
    31.     }
    32. }
    33.  
    Any suggestions?
     
  43. Pixeltoffee

    Pixeltoffee

    Joined:
    Jun 22, 2015
    Posts:
    5
    Any suggestions why I can't see the environment prefab in the scene view but in game view everything seems ok?

    I am using Unity Pro 5.1.1f1 on Mac OS X Yosemite 10.10.5.

    Edit: Same problem with 5.2.

    Thanks in advance!
     
    Last edited: Sep 21, 2015
  44. BarbaricSpaceWhale

    BarbaricSpaceWhale

    Joined:
    Aug 26, 2015
    Posts:
    5
    Hello!

    One odd issue I'm having right off the bat is that I can't click on object in scene view to select them. Dragging the mouse and selecting groups of objects works and so does hierarchy view, but just clicking doesn't select objects. This only happens with this project and it happens every time I import the project.
     
  45. Mrinaank

    Mrinaank

    Joined:
    Jul 25, 2015
    Posts:
    14
    Hi,

    First of all I'm using Unity 5.2 with Win 10 and Amd Radeon Graphics (2GB).

    Everytime my scene is loaded from within the game (both when I die and when use a script I created to access it from my start menu) the game is darker than in my game view in the editor and when I play by pressing the play button in the editor.

    Here are some screen shots.

    When run with the editor :- http://prntscr.com/8iegjv

    When loaded with the help of Application.LoadLevel() :- http://prntscr.com/8iegq7

    Any help would be appreciated.

    Thx in advance!!!
     
  46. IanSmellis

    IanSmellis

    Joined:
    Nov 2, 2014
    Posts:
    26
    There is a post on page 2 I think that tells you have to fix this
     
  47. Naglor

    Naglor

    Joined:
    Sep 22, 2015
    Posts:
    1
    Hey Guys!

    Just started with trying to make my own game :) So far the program is great, and the tutorial is really helpful!
    I just finished the part of Player Health and everything seems to be going fine. Other then at the moment of losing all health, the player isn't dying. I get the following error message:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerHealth.Death () (at Assets/Scripts/Player/PlayerHealth.cs:86)
    PlayerHealth.TakeDamage (Int32 amount) (at Assets/Scripts/Player/PlayerHealth.cs:75)
    EnemyAttack.Attack () (at Assets/Scripts/Enemy/EnemyAttack.cs:81)
    EnemyAttack.Update () (at Assets/Scripts/Enemy/EnemyAttack.cs:60)

    Could someone explain to me how i could fix this?
    Thanks in advance!
     
  48. PuffyZA

    PuffyZA

    Joined:
    Sep 17, 2015
    Posts:
    1
    Hi All,

    For those having trouble with the player character turning, setting the Transform scale.z = 1 on the Floor object fixed the issue I was having. For some reason mine was set to 0 and this breaks the ray casting.

    Naglor:
    You're trying to use a reference to an object where you haven't told it specifically which object it should look at. Check that you've instantiated all the correct variables in the Awake() function, and also that you've linked any public variables (AudioClip maybe?) in the inspector.
     
    Last edited: Sep 23, 2015
  49. kenten132

    kenten132

    Joined:
    Sep 24, 2015
    Posts:
    1
    I have the exact same problem alongside my mouse not making a raycast and rotating the character.
     
  50. Mr.Mille

    Mr.Mille

    Joined:
    Sep 17, 2015
    Posts:
    21
    Great Tutorial, thank you! The only one thing that missing is the explanation "Who" invokes the function StartSinking() in the EnemyHealth script.It's reeeeally difficult to find out if you are new to Unity.