Search Unity

ETeeskiTutorials Scripts up to FPS1.16

Discussion in 'Community Learning & Teaching' started by eteeski, Dec 13, 2011.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Hi guys, I've gotten a lot of requests to post up my scripts from the FPS1 tutorial set. I wanted to wait til it was all done so I could post them as a complete whole, but it seems there is a need to post them sooner. Here are all the scripts up to FPS1.16 . Most of these scripts will still get some additions and tweaks through out the next videos until FPS1 is complete. Also note, there are some key steps to making these scripts work. They wont just work on their own. In the final set of scripts, I'll post the complete scripts and all the key steps you need to make them work. For now, these should be used as a reference to the FPS1 tutorial set on ETeeskiTutorials on youtube. If you haven't seen my tutorials yet and you'd like to join in on making a first person shooter, check out my youtube channel youtube.com/eteeskitutorials and check out the playlist fps1.
    FPS1.1 first video:
    http://www.youtube.com/watch?v=MPN5kYkdYUY
    FPS1.16 sample video:


    PlayerMovementScript:
    Code (csharp):
    1.  
    2. var walkAcceleration : float = 5;
    3. var walkAccelAirRacio : float = 0.1;
    4. var walkDeacceleration : float = 5;
    5. @HideInInspector
    6. var walkDeaccelerationVolx : float;
    7. @HideInInspector
    8. var walkDeaccelerationVolz : float;
    9.  
    10. var cameraObject : GameObject;
    11. var maxWalkSpeed : float = 20;
    12. @HideInInspector
    13. var horizontalMovement : Vector2;
    14.  
    15. var jumpVelocity : float = 20;
    16. @HideInInspector
    17. var grounded : boolean = false;
    18. var maxSlope : float = 60;
    19.  
    20. function LateUpdate ()
    21. {
    22.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    23.     if (horizontalMovement.magnitude > maxWalkSpeed)
    24.     {
    25.         horizontalMovement = horizontalMovement.normalized;
    26.         horizontalMovement *= maxWalkSpeed;    
    27.     }
    28.     rigidbody.velocity.x = horizontalMovement.x;
    29.     rigidbody.velocity.z = horizontalMovement.y;
    30.    
    31.     if (grounded){
    32.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    33.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    34.    
    35.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    36.    
    37.     if (grounded)
    38.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    39.     else
    40.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    41.            
    42.     if (Input.GetButtonDown("Jump")  grounded)
    43.         rigidbody.AddForce(0,jumpVelocity,0);
    44. }
    45.  
    46. function OnCollisionStay (collision : Collision)
    47. {
    48.     for (var contact : ContactPoint in collision.contacts)
    49.     {
    50.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    51.             grounded = true;
    52.     }
    53. }
    54.  
    55. function OnCollisionExit ()
    56. {
    57.     grounded = false;
    58. }
    59.  
    MouseLookScript:
    Code (csharp):
    1.  
    2. var defaultCameraAngle : float = 60;
    3. @HideInInspector
    4. var currentTargetCameraAngle : float = 60;
    5. @HideInInspector
    6. var racioZoom : float = 1;
    7. @HideInInspector
    8. var racioZoomV : float;
    9.  
    10. var racioZoomSpeed : float = 0.2;
    11.  
    12. var lookSensitivity : float = 5;
    13. @HideInInspector
    14. var yRotation : float;
    15. @HideInInspector
    16. var xRotation : float;
    17. @HideInInspector
    18. var currentYRotation : float;
    19. @HideInInspector
    20. var currentXRotation : float;
    21. @HideInInspector
    22. var yRotationV : float;
    23. @HideInInspector
    24. var xRotationV : float;
    25. var lookSmoothDamp : float = 0.1;
    26. @HideInInspector
    27. var currentAimRacio : float = 1;
    28.  
    29. function Update ()
    30. {
    31.     if (currentAimRacio == 1)
    32.         racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
    33.     else
    34.         racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
    35.        
    36.     camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    37.  
    38.     yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    39.     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    40.    
    41.     xRotation = Mathf.Clamp(xRotation, -90, 90);
    42.    
    43.     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    44.     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    45.    
    46.     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    47. }
    48.  
    GunScript:
    Code (csharp):
    1.  
    2. var cameraObject : GameObject;
    3. @HideInInspector
    4. var targetXRotation : float;
    5. @HideInInspector
    6. var targetYRotation : float;
    7. @HideInInspector
    8. var targetXRotationV : float;
    9. @HideInInspector
    10. var targetYRotationV : float;
    11.  
    12. var rotateSpeed : float = 0.3;
    13.  
    14. var holdHeight : float = -0.5;
    15. var holdSide : float = 0.5;
    16. var racioHipHold : float = 1;
    17. var hipToAimSpeed : float = 0.1;
    18. @HideInInspector
    19. var racioHipHoldV : float;
    20.  
    21. var aimRacio : float = 0.4;
    22.  
    23. var zoomAngle : float = 30;
    24.  
    25. var fireSpeed : float = 15;
    26. @HideInInspector
    27. var waitTilNextFire : float = 0;
    28. var bullet : GameObject;
    29. var bulletSpawn : GameObject;
    30.  
    31. var shootAngleRandomizationAiming : float = 5;
    32. var shootAngleRandomizationNotAiming : float = 15;
    33.  
    34. var recoilAmount : float = 0.5;
    35. var recoilRecoverTime : float = 0.2;
    36. @HideInInspector
    37. var currentRecoilZPos : float;
    38. @HideInInspector
    39. var currentRecoilZPosV : float;
    40.  
    41.  
    42. function Update ()
    43. {
    44.     if (Input.GetButton("Fire1"))
    45.     {
    46.         if (waitTilNextFire <= 0)
    47.         {
    48.             if (bullet)
    49.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    50.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    51.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    52.             currentRecoilZPos -= recoilAmount;
    53.             waitTilNextFire = 1;
    54.         }
    55.     }
    56.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    57.  
    58.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    59.  
    60.     cameraObject.GetComponent(MouseLookScript).current  TargetCameraAngle = zoomAngle;
    61.  
    62.     if (Input.GetButton("Fire2")){
    63.         cameraObject.GetComponent(MouseLookScript).current  AimRacio = aimRacio;
    64.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    65.     if (Input.GetButton("Fire2") == false){
    66.         cameraObject.GetComponent(MouseLookScript).current  AimRacio = 1;
    67.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    68.  
    69.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    70.    
    71.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    72.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    73.    
    74.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    75. }
    76.  
    BulletScript:
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5.  
    6. function Update ()
    7. {
    8.     var hit : RaycastHit;
    9.     if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
    10.     {
    11.         if (decalHitWall  hit.transform.tag == "Level Parts")
    12.             Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    13.     }
    14.     Destroy(gameObject);
    15. }
    16.  
    DestroyAfterTimeScript:
    Code (csharp):
    1.  
    2. var destroyAfterTime : float = 30;
    3. var destroyAfterTimeRandomization : float = 0;
    4. @HideInInspector
    5. var countToTime : float;
    6.  
    7. function Awake ()
    8. {
    9.     destroyAfterTime += Random.value * destroyAfterTimeRandomization;
    10. }
    11.  
    12. function Update ()
    13. {
    14.     countToTime += Time.deltaTime;
    15.     if (countToTime >= destroyAfterTime)
    16.         Destroy(gameObject);
    17. }
    18.  
    Here's what the Hierarchy should look like in your game. It does matter where the scripts are and what objects are parents and children of other objects:
    View attachment 29513
     

    Attached Files:

    Last edited: Jan 16, 2012
  2. linkentz

    linkentz

    Joined:
    Dec 14, 2011
    Posts:
    1
    this tutorial is so good .i learned so much things.plz keep making new tutorial plz.my master
     
  3. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    hahaha my master? lol that's got to be the best compliment on my tutorials so far. But yes, never fear, I plan on coming out with new tutorials indefinitely or until people stop watching them. :)
     
    Dante Kurosaki likes this.
  4. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    I am watching your lessons are amazing

    How do I put the machine gun sound

    Thank you,hero
    Thank you,hero
     
  5. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    Please Upload the package Please Please Please Please
     
  6. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    Please Upload the package Please Please Please Please
     
  7. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    No prob, dude. I uploaded it into my first comment on this thread.
     
  8. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    You are a great man deserves all the love and appreciation
     
  9. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Thanks man, you're too kind.
     
  10. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    hi man can you make the gun Script reload the Clip if I press ("r")
     
  11. Jingee

    Jingee

    Joined:
    Dec 17, 2011
    Posts:
    1
    Hi, I have been following your tutorials thoroughly and have found no mistakes in my script so i copied yours and i got the same error. I am currently on your move friction tutorial and so i only have "PlayerMovementScript" and "MouseLookScript" and i am wondering if either of those scripts require a certain component that i am missing? The compilation error states:

    $for eteesji.png

    Thank you for making such great tutorials.!
    KEEP UP THE GOOD WORK!!!! :D
     
  12. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Thank you very much for catching that. The problem was that somehow, some spaces were added between "current" and "YRotation" when it should have been "currentYRotation". So the line should read:

    transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);

    Fixed it in the first post too. Thanks man :)
     
  13. Camando360

    Camando360

    Joined:
    Aug 18, 2011
    Posts:
    5
    Hey man, thanks a lot for these tuts, could you post just the recoil script that i could attach to my main camera of an FPS controller?
     
  14. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    I don't think these scripts will be easily broken up and work in a pretty specific way with the game objects. If you explain to me how your objects are related and where the scripts are and what functions they have, I might be able to come up with a quick bit of code that'll work. For example, if the gun a child of the main camera? What script is in charge of firing bullets when the mouse button is clicked? Etc etc.
     
  15. omarzonex

    omarzonex

    Joined:
    Jan 16, 2012
    Posts:
    158
    WoW ....

    Very Very Very Nice

    Working Tutorials Beautifull 10000000%

    Realistic GunShot Speed (Realism Nice)

    I Love you Work Game Gun

    ok
     
    Last edited: Feb 1, 2012
  16. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Ten million percent beautiful? haha thank man! Glad you liked them :)
     
  17. Echokage

    Echokage

    Joined:
    Feb 9, 2012
    Posts:
    1
    i'm having a few issues...



    Help please? i'm a Unity nooblet :\
     
  18. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    you are amazing reality
     
    Last edited: Feb 18, 2012
  19. CoolMan53

    CoolMan53

    Joined:
    Feb 20, 2012
    Posts:
    1
    Hey great tuts im new to scripting and i copied the gun script strait to my project and it said to insert a ';' but there was already one there. PLEASE HELP !!!
     
    Last edited: Feb 21, 2012
  20. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    thanks man :)
     
  21. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    you can't copy and paste these scripts into other projects. The gun script is constantly communicating with the playermovementscript and the camerascript every frame. All of these scripts work together as a whole, you really cant break them apart like that.
     
  22. Unity.Darren

    Unity.Darren

    Joined:
    Jul 9, 2011
    Posts:
    7
    I don't get when the tutorial starts. I went on your first uploaded video and it seems as if it's always half way done.
     
  23. XIKO

    XIKO

    Joined:
    Dec 6, 2011
    Posts:
    10
    hi boy! Thank you, thank you for the videos. You're a great teacher. Congratulations!
    I would like to know more about the sound of steps, you will talk about it?

    Hugs and sorry for my bad English, I am Brazilian.
     
  24. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    You are the Great White Knight
     
    Last edited: Feb 27, 2012
  25. love

    love

    Joined:
    Dec 14, 2011
    Posts:
    16
    I love you You are my idol

    We are nothing without you in unity
     
    Last edited: Feb 27, 2012
  26. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    oops, sorry man, i should have made that more clear. FPS1.1 is the first episode. I showed FPS1.16 (the 16th episode) in this forum post because I wanted to show off the quality of (at the time) the latest episode because the first few episodes don't have as good of video quality as my more recent ones.
     
  27. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Thanks man :) I will look into footsteps and see if i can make a simple script for footsteps. Maybe I could sync it with the head and gun bobbing... but that would add a good bit of polish to the game.
     
  28. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    haha dude, thank you. I'm glad you like my videos so much. I really appreciate all the complement you've given me on these forums :)
     
  29. Bulletghost4

    Bulletghost4

    Joined:
    Apr 29, 2012
    Posts:
    3
    hi, having a few problems wiith my code could you take a look please, i cant jump at all :S

    var walkAcceleration : float = 5;
    var cameraObject : GameObject;
    var maxWalkSpeed : float = 20;
    @HideInInspector
    var horizontalMovement : Vector2;
    var jumpVelocity : float = 20;
    @HideInInspector
    var grounded : boolean = false;
    var maxSlope : float = 60;

    function Update () {

    horizontalMovement = Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
    if (horizontalMovement.magnitude > maxWalkSpeed)
    {
    horizontalMovement = horizontalMovement.normalized;
    horizontalMovement *= maxWalkSpeed;
    }
    rigidbody.velocity.x = horizontalMovement.x;
    rigidbody.velocity.z = horizontalMovement.y;
    transform.rotation=Quaternion.Euler(0,cameraObject.GetComponent(MouseLookScript).currentYRotation,0);
    rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration,0,Input.GetAxis("Vertical") * walkAcceleration);

    if (Input.GetButtonDown("Jump") grounded)
    rigidbody.AddForce(0,jumpVelocity,0);
    }

    function OnCollionStay (collision : Collision)
    {
    for (var contact : ContactPoint in collision.contacts)
    {
    if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    grounded = true;

    }
    }

    function OnCollisionExit ()

    {
    grounded = false;
    }

    thanks :D
     
  30. Bulletghost4

    Bulletghost4

    Joined:
    Apr 29, 2012
    Posts:
    3
    oh and great videos sorry i fogot to add that, they really are the best!
     
  31. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    thanks :)
    Your code looks mostly fine. You do have a typo with the word "Horizont al", but that shouldn't effect jumping. Have you tried setting jump velocity to something really large, like 1000000. If you're code is working, you'll fly off the map.
     
  32. Denninho

    Denninho

    Joined:
    May 9, 2012
    Posts:
    2
    Hei,
    have done all Tutorials to the part with Weapon. Allways have some problems, that the player ist to slow etc.
    Now I have downloaded the zip File which you have posted. Now my problem is, that the "player" can rotate but can't walk. And my gun is rotating very strange and in play mode i cant see.
    Sry for my bad english, I am from Germany! ;)
    Greetz
     
  33. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    it sounds like you just need to increase the walkAcceleration variable in the inspector. try setting it to a really really big number.
     
  34. Denninho

    Denninho

    Joined:
    May 9, 2012
    Posts:
    2
    Ok, now I can walk. Thanks!
    But my second problem is these, that my weapon is scaling in the play mode automaticly from 0.4 to 1. And is rotating around his rotation axe.
    Greetz

    /EDIT: I have taken some screenshots, I think its easyier to understand^^



     
    Last edited: May 10, 2012
  35. flavur

    flavur

    Joined:
    Jan 10, 2012
    Posts:
    4
    I know that this tutorial is in javascript but I'm doing it in c# and I keep getting this error:
    error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    this error is caused by the playerMovementScript on line 30 where you normalize the horizontalMovement horizontalMovement.normalized;

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playerMovementScript : MonoBehaviour
    6. {
    7.     public float walkAcceleration = 5.0f;
    8.     public GameObject cameraObject;
    9.     public float maxWalkSpeed = 5;
    10.     [HideInInspector]
    11.     public Vector2 horizontalMovement;
    12.     [HideInInspector]
    13.     public bool grounded = false;
    14.     public float maxSlope;
    15.     public float jumpVelocity = 20.0f;
    16.    
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         horizontalMovement = Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
    27.         //checks if horizontal movement is more than maxWalkSpeed
    28.         if(horizontalMovement.magnitude > maxWalkSpeed)
    29.         {
    30.             //returns the normalized value would
    31.             horizontalMovement.normalized;
    32.             horizontalMovement *= maxWalkSpeed;
    33.         }
    34.        
    35.         rigidbody.velocity.x = horizontalMovement.velocity.x;
    36.         rigidbody.velocity.z = horizontalMovement.velocity.y;
    37.        
    38.         transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent<mouseLookScript>().currentYRotation,0);
    39.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*walkAcceleration,0,Input.GetAxis("Vertical")*walkAcceleration);
    40.        
    41.         //did the player press the space bar and are they grounded
    42.         if(Input.GetButtonDown("Jump") grounded)
    43.         {
    44.             //if the player is grounded
    45.             //the player can jump as high as the jumpVelocity
    46.             rigidbody.AddRelativeForce(0,jumpVelocity,0);
    47.         }
    48.     }
    49.    
    50.     void OnCollisionStay(Collision collision)
    51.     {
    52.         foreach(ContactPoint contact in collision.contacts)
    53.         {
    54.             if(Vector3.Angle(contact.normal,Vector3.up) < maxSlope)
    55.             {
    56.                 grounded = true;
    57.             }
    58.         }
    59.     }
    60.    
    61.     void OnCollisionExit()
    62.     {
    63.         grounded = false;
    64.     }
    65. }
    66. using UnityEngine;
    67. using System.Collections;
    68.  
    69. public class playerMovementScript : MonoBehaviour
    70. {
    71.     public float walkAcceleration = 5.0f;
    72.     public GameObject cameraObject;
    73.     public float maxWalkSpeed = 5;
    74.     [HideInInspector]
    75.     public Vector2 horizontalMovement;
    76.     [HideInInspector]
    77.     public bool grounded = false;
    78.     public float maxSlope;
    79.     public float jumpVelocity = 20.0f;
    80.    
    81.     // Use this for initialization
    82.     void Start ()
    83.     {
    84.    
    85.     }
    86.    
    87.     // Update is called once per frame
    88.     void Update ()
    89.     {
    90.         horizontalMovement = Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
    91.         //checks if horizontal movement is more than maxWalkSpeed
    92.         if(horizontalMovement.magnitude > maxWalkSpeed)
    93.         {
    94.             //returns the normalized value would
    95.             horizontalMovement.normalized;
    96.             horizontalMovement *= maxWalkSpeed;
    97.         }
    98.        
    99.         rigidbody.velocity.x = horizontalMovement.velocity.x;
    100.         rigidbody.velocity.z = horizontalMovement.velocity.y;
    101.        
    102.         transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent<mouseLookScript>().currentYRotation,0);
    103.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*walkAcceleration,0,Input.GetAxis("Vertical")*walkAcceleration);
    104.        
    105.         //did the player press the space bar and are they grounded
    106.         if(Input.GetButtonDown("Jump") grounded)
    107.         {
    108.             //if the player is grounded
    109.             //the player can jump as high as the jumpVelocity
    110.             rigidbody.AddRelativeForce(0,jumpVelocity,0);
    111.         }
    112.     }
    113.    
    114.     void OnCollisionStay(Collision collision)
    115.     {
    116.         foreach(ContactPoint contact in collision.contacts)
    117.         {
    118.             if(Vector3.Angle(contact.normal,Vector3.up) < maxSlope)
    119.             {
    120.                 grounded = true;
    121.             }
    122.         }
    123.     }
    124.    
    125.     void OnCollisionExit()
    126.     {
    127.         grounded = false;
    128.     }
    129. }
    130.  
    if someone could tell me how to fix this problem and what causes it, I would be extremely thankful.
     
  36. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    change
    horizontalMovement.normalized;
    to
    horizontalMovement.Normalize();
     
  37. flavur

    flavur

    Joined:
    Jan 10, 2012
    Posts:
    4
    thanks for the help man that got rid of that error but now I have about 13 new errors to try and figure out
     
  38. flavur

    flavur

    Joined:
    Jan 10, 2012
    Posts:
    4
    Hey ETeeski I've completed up to FPS1.12 and my character won't walk he just floats around the level and all I can do is control the camera could you take a look at my code and help me try and fix this problem. And thanks for making these tutorials they are really helpful.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playerMovementScript : MonoBehaviour
    6. {
    7.     //BALLPARK VARIABLES
    8.     [HideInInspector]
    9.     public float xWalkDeaccelerationVel;
    10.     [HideInInspector]
    11.     public float zWalkDeaccelerationVel;
    12.     public float walkAcceleration = 5.0f;
    13.     public float walkDeacceleration = 5.0f;
    14.     public float walkAccelRatio = 0.1f;
    15.    
    16.     public GameObject cameraObject;
    17.     public float maxWalkSpeed = 20.0f;
    18.     [HideInInspector]
    19.     public Vector2 horizontalMovement;
    20.    
    21.     [HideInInspector]
    22.     public bool grounded = false;
    23.     public float maxSlope = 60.0f;
    24.     public float jumpVelocity = 20.0f;
    25.    
    26.     // Use this for initialization
    27.     void Start ()
    28.     {
    29.    
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update ()
    34.     {
    35.         horizontalMovement = new Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
    36.         //checks if horizontal movement is more than maxWalkSpeed
    37.         if(horizontalMovement.magnitude > maxWalkSpeed)
    38.         {
    39.             //returns the normalized value would
    40.             horizontalMovement.Normalize();
    41.             //makes sure horizontalMovement is always
    42.             //set to maxWalkSpeed
    43.             horizontalMovement *= maxWalkSpeed;
    44.         }
    45.         //makes the player move at the speed we want them to move
    46.         Vector3 velPos = rigidbody.velocity;
    47.         velPos.x = horizontalMovement.x;
    48.         velPos.z = horizontalMovement.y;
    49.        
    50.        
    51.         if(grounded)
    52.         {
    53.             //gives the character movement using a,s,w,d based time.deltaTime and not the computer's framerate
    54.             rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*walkAcceleration*Time.deltaTime,0,Input.GetAxis("Vertical")*walkAcceleration*Time.deltaTime);
    55.         }
    56.         else
    57.         {
    58.             rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*walkAccelRatio*Time.deltaTime,0,Input.GetAxis("Vertical")*walkAccelRatio*Time.deltaTime);
    59.         }
    60.        
    61.         //makes sure that the camera turns with the character
    62.         transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent<mouseLookScript>().currentYRotation,0);
    63.        
    64.         //did the player press the space bar and are they grounded
    65.         if(Input.GetButtonDown("Jump") grounded)
    66.         {
    67.             //if the player is grounded
    68.             //the player can jump as high as the jumpVelocity
    69.             rigidbody.AddRelativeForce(0, jumpVelocity, 0);
    70.         }
    71.        
    72.         if(grounded)
    73.         {
    74.             //This slows down the player movement by adding friction
    75.             //create a velocity variable and smoothDamp uses that variable
    76.             //to create a smooth movement
    77.             //it also makes sure that it is not relying on the framerate
    78.            
    79.             velPos.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref xWalkDeaccelerationVel, walkDeacceleration);
    80.             velPos.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref zWalkDeaccelerationVel, walkDeacceleration);
    81.         }
    82.     }
    83.    
    84.     //function that checks for collision
    85.     void OnCollisionStay(Collision collision)
    86.     {
    87.         //checks whenever the player touches the ground
    88.         foreach(ContactPoint contact in collision.contacts)
    89.         {
    90.             //if the angle b/w the normal of the contact points and
    91.             //(0,1,0) is less than maxSlope then the player is touching
    92.             //the ground
    93.             if(Vector3.Angle(contact.normal,Vector3.up) < maxSlope)
    94.             {
    95.                 grounded = true;
    96.             }
    97.         }
    98.     }
    99.    
    100.     //function that sets grounded back to false when the player
    101.     //is not on the ground
    102.     void OnCollisionExit()
    103.     {
    104.         grounded = false;
    105.     }
    106. }
    107.  
     
  39. dmaster

    dmaster

    Joined:
    Jun 8, 2012
    Posts:
    3
    Help when i did the Gun script these error messages displayed:



    and there is semicolons on the end
     
  40. dmaster

    dmaster

    Joined:
    Jun 8, 2012
    Posts:
    3
    sorry bout picture this one:

     
  41. dmaster

    dmaster

    Joined:
    Jun 8, 2012
    Posts:
    3
    Bigger Pic:

     
  42. konnosgar

    konnosgar

    Joined:
    Jun 19, 2012
    Posts:
    1
    My gun rotatates somehow strange in the x axis. it rotates in its x axis.
     
  43. Zwero

    Zwero

    Joined:
    Jun 26, 2012
    Posts:
    7
    ME: I wanna make a game JavaScriptKnowlege:0,000001 ..... hmmm i must to learn some new things ..... *after 6 ETeeski tutorials* ok so ... this is wrong writed xD ... ill try to modify like .... yep is working :) JavScriptKnowlege: 10% :) i learned lots of thing after your tutorials .... i don't speak english verry well but i undersand all that are you saying in the videos .... i love that when you are in Unity3D you still explain what are you doing ... sa saw some videos that when they work you just copy like a BOT and you don't understant what are you doing ... Verry epics tutorials ... *Notch approves* WTF!? xD NOTCH :)
     
  44. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    lol thank you :)
     
  45. python01

    python01

    Joined:
    Jul 10, 2012
    Posts:
    3
    Great tutorial series, its what Ive been looking for and definitely the best on the web!!

    the only issue im having is that when i added Time.deltatime to the movement script, i would not slow down at all. it was working fine before. I tried setting walkdeacellerateration to a really large amount, but it still didn't work
    love the series, keep it up :D:D
     
  46. Zwero

    Zwero

    Joined:
    Jun 26, 2012
    Posts:
    7
    Hey eteeski ... i have a problem my sistem deleted all my work and i downloaded fps1.16 but when i press play my char is auto-waling in left .... i'm no touching anything he just walk .... please help ... oh yeah and i cant jump cuz of this
     
  47. StealthSocky

    StealthSocky

    Joined:
    Jul 17, 2012
    Posts:
    1
    Hey... Great tutorials... that I have been seeing... I copied and pasted the PlayerMovementScript into my game... and it wont let me move... or jump, etc. The MouseLookScript works fine but I was wondering what may be the problen with it... or if it just my game engine.
     
  48. pugdug808

    pugdug808

    Joined:
    Jul 24, 2012
    Posts:
    3
    i copy and pasted the code cause i was having major issues with my own but after copying all the scripts it says it needs a semi colon on three different lines of code but there already is one there (since it is copied) i cant fix it. please help!


    EDIT i downloaded the project file and i set everything up but my guy wont move i turned up the acceleration and speed a bunch and i can move now but the screen shakes like crazy
     
    Last edited: Jul 24, 2012
  49. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    try turning down the headbob speed on the main camera
     
  50. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    have you tried adjusting all the variables in the inspector?