Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ETeeskiTutorials FPS1 Problem Diagnostic Collaboration Thread

Discussion in 'Community Learning & Teaching' started by eteeski, Mar 5, 2012.

  1. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Okay, simple test to see whether it's your code or your setup - attach the gunscript to a small cube. If the code works, it will move to the center of the screen when you ADS. Post a picture, and we'll go from there.
     
  2. SubjectSix

    SubjectSix

    Joined:
    Mar 12, 2013
    Posts:
    6
  3. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Okay, see at 2:22, the pivot point is NOT at the gun's sights? That's the problem. You need to make a separate game object, attach the gun script to it, make the gun a child of that object and move it so that the gun's pivot point is where your eyes should be when aiming down the sights.
     
  4. SubjectSix

    SubjectSix

    Joined:
    Mar 12, 2013
    Posts:
    6
    THANK YOU!
    Dude. That helped so much! :D :D :D :D
    Do I have the right to kiss you?
    Was that gay?
    S***...sorry...
    Uhm... Thanks...
    Bye.
     
  5. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    sorry if this got answered already but try having your current gun in your player movement script the weapon your holding(or if your not holding any have a random weapon)

     
  6. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    move the model of the gun object forward(not THE gun that has the gun script but the model)
     
  7. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    have the bullet spawn var an array(in your gun script) (by saying: var bulletSpawn : GameObject[];)
    (please note that for where in your gun script where you call bulletspawn put [0] or [1] etc.)

     
  8. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    is your bulletSpawn rotated?if it is not try get the blue axis facing the same direction of your gun
     
  9. ImNew2Unity

    ImNew2Unity

    Joined:
    Feb 2, 2013
    Posts:
    21
    FPS 1.8


    I have a problem when i look up and down, my gun rotates when i look up and down, i took a picture showing what i mean the first one is looking straight and the second is looking up

    tinypic.com/r/142tr3r/6
     
    Last edited: Apr 6, 2013
  10. TheLosmy

    TheLosmy

    Joined:
    Apr 6, 2013
    Posts:
    2
  11. Dreprodigy

    Dreprodigy

    Joined:
    Apr 23, 2013
    Posts:
    4
    FPS1.3; So i decided to start over after to running into problems on 1.8. My current issue is that the camera shakes violently when I move the player capsule. This never happened the first time I followed the tutorial. How can I fix this?
     
  12. Dreprodigy

    Dreprodigy

    Joined:
    Apr 23, 2013
    Posts:
    4
    Fixed my problem by changing 'function Update' to 'function LateUpdate'. Im still new to coding with unity.
     
  13. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Just some advice: when it comes to moving rigidbodies, make sure everything is in fixed update.
     
  14. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    i have problem at FPS1.41

    my enemy moves like teleport of slender.i mean that he is moving right-left everywhere in the maze ,inside of boxes and cant be killed
    any solution?
     
  15. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Did you set the rigidbodies to is kinematic?
     
  16. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    yes all game objects(left arm empty etc) are kinematic and use gravity
     
  17. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    do you have any other solve in your mind?
     
  18. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Do you have a video/screenshots and code?
     
  19. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    EnemyBodyPartScript:
    var enemyBody : GameObject;
    var addForceVector : Vector3;
    var damageMultiplyer : float = 1;

    function Update ()
    {
    if (addForceVector != Vector3.zero !enemyBody.GetComponent(EnemyBodyScript).enabled)
    {
    if (rigidbody)
    rigidbody.AddForce(addForceVector);
    else
    transform.parent.rigidbody.AddForce(addForceVector);
    addForceVector = Vector3.zero;
    }




    EnemyBodyScript:

    var moveDamp : float = 0.1;
    var parentToFollow : Transform;
    var parentLastPos : Vector3;
    var targetRotation : Quaternion;
    var turnSpeed : float = 10;

    var velocityX : float;
    var velocityZ : float;

    var parentScript : EnemyMovementScript;
    var walkAnimationString : String;
    var runAnimationString : String;

    var enemyHealth : float = 1;
    var rigidbodies : Rigidbody[];

    function Awake ()
    {
    parentScript = transform.parent.GetComponent(EnemyMovementScript);
    animation.wrapMode = WrapMode.Loop;
    parentToFollow = transform.parent;
    transform.parent = null;
    }

    function Update ()
    {
    transform.position.x = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.x, velocityX, moveDamp);
    transform.position.z = Mathf.SmoothDamp(transform.position.z, parentToFollow.position.z, velocityZ, moveDamp);

    var tempParentCurrentPos : Vector3 = parentToFollow.position;
    var tempParentLastPos : Vector3 = parentLastPos;
    tempParentCurrentPos.y = 0;
    tempParentLastPos.y = 0;

    targetRotation = Quaternion.LookRotation(tempParentCurrentPos - tempParentLastPos);
    parentLastPos = parentToFollow.position;

    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);

    var hits : RaycastHit[];
    hits = Physics.RaycastAll(Ray(transform.position + Vector3(0,100,0), Vector3.up * -1));
    for (var i : int = 0; i < hits.length; i++)
    {
    var hit : RaycastHit = hits;
    if (hit.transform.gameObject == parentToFollow.gameObject.GetComponent(EnemyMovementScript).currentCell)
    transform.position.y = hit.point.y;
    }

    if (!animation.IsPlaying(walkAnimationString) !parentScript.aware)
    animation.Play(walkAnimationString);
    if (!animation.IsPlaying(runAnimationString) parentScript.aware)
    animation.Play(runAnimationString);

    if (enemyHealth <= 0)
    {
    for (var rigidbodyFor in rigidbodies)
    rigidbodyFor.isKinematic = false;
    animation.enabled = false;
    parentScript.enabled = false;
    enabled = false;
    }
    }

    EnemyMovementScript:

    var moveDamp : float = 0.1;
    var parentToFollow : Transform;
    var parentLastPos : Vector3;
    var targetRotation : Quaternion;
    var turnSpeed : float = 10;

    var velocityX : float;
    var velocityZ : float;

    var parentScript : EnemyMovementScript;
    var walkAnimationString : String;
    var runAnimationString : String;

    var enemyHealth : float = 1;
    var rigidbodies : Rigidbody[];

    function Awake ()
    {
    parentScript = transform.parent.GetComponent(EnemyMovementScript);
    animation.wrapMode = WrapMode.Loop;
    parentToFollow = transform.parent;
    transform.parent = null;
    }

    function Update ()
    {
    transform.position.x = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.x, velocityX, moveDamp);
    transform.position.z = Mathf.SmoothDamp(transform.position.z, parentToFollow.position.z, velocityZ, moveDamp);

    var tempParentCurrentPos : Vector3 = parentToFollow.position;
    var tempParentLastPos : Vector3 = parentLastPos;
    tempParentCurrentPos.y = 0;
    tempParentLastPos.y = 0;

    targetRotation = Quaternion.LookRotation(tempParentCurrentPos - tempParentLastPos);
    parentLastPos = parentToFollow.position;

    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);

    var hits : RaycastHit[];
    hits = Physics.RaycastAll(Ray(transform.position + Vector3(0,100,0), Vector3.up * -1));
    for (var i : int = 0; i < hits.length; i++)
    {
    var hit : RaycastHit = hits;
    if (hit.transform.gameObject == parentToFollow.gameObject.GetComponent(EnemyMovementScript).currentCell)
    transform.position.y = hit.point.y;
    }

    if (!animation.IsPlaying(walkAnimationString) !parentScript.aware)
    animation.Play(walkAnimationString);
    if (!animation.IsPlaying(runAnimationString) parentScript.aware)
    animation.Play(runAnimationString);

    if (enemyHealth <= 0)
    {
    for (var rigidbodyFor in rigidbodies)
    rigidbodyFor.isKinematic = false;
    animation.enabled = false;
    parentScript.enabled = false;
    enabled = false;
    }
    }
     
  20. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11

    Attached Files:

    Last edited: May 5, 2013
  21. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Posted a response in your thread. :)
     
  22. Alzan

    Alzan

    Joined:
    Sep 2, 2012
    Posts:
    81
  23. sixShOOter50o0

    sixShOOter50o0

    Joined:
    May 31, 2013
    Posts:
    1
    FPS1.8
    Is there any way to make my gun move forward on the z axis? my gun is no clipping in the camera...
    Can someone reply to me something that can do that?

    here's the script:
    I inverted some x's and y's because i was having a backwards effect (the gun was swinging upwards when i was looking around on the x and z axis and viceversa for the other).
     
    Last edited: Jun 2, 2013
  24. LandaVaranda

    LandaVaranda

    Joined:
    Jul 15, 2013
    Posts:
    1
    IDownloaded your PlayerMovementScript and MouseLookScript, but now i cant walk or jump.. HELP!!!!
     
  25. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    can you show me those two scripts?So i can see what is wrong ..
     
  26. 0-_-0

    0-_-0

    Joined:
    Jul 13, 2013
    Posts:
    22
    Hi I need help with a code for Destroy After Time Script

    $Capture.JPG

    That is what the compilation error said
    here is the code

    Code (csharp):
    1. var destroyAfterTime : float = 30;
    2.  
    3. var destroyAfterTimeRandomization : float = 0;
    4.  
    5. @HideInInspector
    6.  
    7. var countToTime : float;
    8.  
    9.  
    10.  
    11. function Awake ()
    12.  
    13. {
    14.  
    15.     destroyAfterTime += Random.value * destroyAfterTimeRandomization;
    16.  
    17. }
    18.  
    19.  
    20.  
    21. function Update ()
    22.  
    23. {
    24.  
    25.     countToTime += Time.deltaTime;
    26.  
    27.     if (countToTime >= destroyAfterTime)
    28.  
    29.         Destroy(gameObject);
    30.         }
    31.     }
    32. }
     
    Last edited: Jul 16, 2013
  27. Nick98

    Nick98

    Joined:
    Mar 6, 2013
    Posts:
    11
    you need just that : .......
    if (countToTime >= destroyAfterTime)



    Destroy(gameObject);

    }


    only one bracket!!
     
  28. Kasigawa

    Kasigawa

    Joined:
    Aug 18, 2012
    Posts:
    6
    ::sorry still new to this::

    Hey great tutorials! I just have a question. I've gone through your tutorials to make a simple player control (move, jump, and crouch) using your tutorials. Even though everything works fine, the only problem I have encountered is the player's jumps are very glitchy. By glitchy I mean the player jumps at different distances each time. Is there a way I can clean this up?

    Here's my code:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var walkAcceleration : float = 5;
    5. var walkAccelAirRacio : float = 0.1;
    6. var walkDeacceleration : float = 5;
    7. @HideInInspector
    8. var walkDeaccelerationVolx : float;
    9. @HideInInspector
    10. var walkDeaccelerationVolz : float;
    11.  
    12. var cameraObject : GameObject;
    13. var maxWalkSpeed : float = 20;
    14. @HideInInspector
    15. var horizontalMovement : Vector2;
    16.  
    17. var jumpVelocity : float = 20;
    18.  
    19. @HideInInspector
    20. var grounded : boolean = false;
    21. var maxSlope : float = 60;
    22.  
    23. var crouchRacio : float = 0.3;
    24. var transitionToCrouchSec : float = 0.2;
    25. var crouchingVelocity : float;
    26. var currentCrouchRacio : float = 1;
    27. var origionalLocalScaleY : float;
    28. var crouchLocalScaleY : float;
    29.  
    30. var collisionDetectionSphere : GameObject;  //for crouching under an object
    31.  
    32. function Start () {
    33.  
    34. }
    35.  
    36. function Awake(){
    37.     currentCrouchRacio = 1;
    38.     origionalLocalScaleY = transform.localScale.y;
    39.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    40. }
    41.  
    42.  
    43.  
    44. function Update () {
    45.  
    46.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY,origionalLocalScaleY,currentCrouchRacio);
    47.     if(Input.GetButton("Crouch"))
    48.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio,0,crouchingVelocity,transitionToCrouchSec);
    49.     if(Input.GetButton("Crouch")== false  collisionDetectionSphere.GetComponent(collisionDetectionSphereScript).collisionDetected == false)//crouch under objects
    50.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio,1,crouchingVelocity,transitionToCrouchSec);
    51.  
    52.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    53.     if(horizontalMovement.magnitude > maxWalkSpeed){
    54.         horizontalMovement = horizontalMovement.normalized;
    55.         horizontalMovement *= maxWalkSpeed;
    56.     }
    57.     rigidbody.velocity.x = horizontalMovement.x;
    58.     rigidbody.velocity.z = horizontalMovement.y;
    59.    
    60.     //for deacceleration
    61.     if(grounded){
    62.     rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x,0, walkDeaccelerationVolx, walkDeacceleration);
    63.     rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z,0, walkDeaccelerationVolz, walkDeacceleration);
    64.     }
    65.    
    66.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MyPlayerLook).currentYRotation, 0);
    67.     if(grounded)
    68.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    69.     else
    70.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    71.  
    72.     if(Input.GetButtonDown("Jump") grounded)
    73.     rigidbody.AddForce(0,jumpVelocity,0);
    74.  
    75. }
    76.  
    77.  
    78. function OnCollisionStay(collision : Collision){
    79.     for(var contact : ContactPoint in collision.contacts){
    80.         if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    81.         grounded = true;
    82.     }
    83. }
    84.  
    85. function OnCollisionExit(){
    86.     grounded = false;
    87. }
    88.  
    89.  
     
  29. chainsn4tcherr

    chainsn4tcherr

    Joined:
    Feb 16, 2013
    Posts:
    1
    Actually, set the Walk Speed to 500. You just need to put values a li'l higher, you'll see :)
     
  30. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Hi, love your tutorials.
    I stumbled over your AI pathfinding tuts when I was trying to make my first AI system. I implemented them into my game, but none of the other code is from your tutorial.

    I'm on FPS 1.34 - AI Path Finding - Enemy Reads Map.
    I have the system made, all the errors worked out. The problem is, no matter where in my map I am (even if I'm not in a cell), the goalDoor is always a single one of my doors. Now, when I move around the map, the goal door will change, so that there will be two doors as the goal door.

    (Example: The cell my enemy is in has 2 doors on it, door1 and door2. Door1 is ALWAYs active, whether I am closer to door 1 or door 2. Now, if I'm closer to door 1, door 2 will not be active.)

    Do you know why this would be happening?

    Here are all my (pertinent) scripts:
    AIPathCellScript.js
    AIPathDoorScript.js
    GenericEnemyBehavior.js
    PlayerBehavior.js


    Thanks for the help man!
     
  31. Jett Dela Cruz

    Jett Dela Cruz

    Joined:
    Jul 31, 2013
    Posts:
    7
    i'm stuck debugging this error:
    EnemyMovementScript.js(34,8: BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'. it works fine on PC MAC, but on android that error appears.. can someone help me here??


    EnemyMovementScript:

    var currentCell : GameObject;
    var playerCar : PlayerCar;
    var playerTransform : Transform;
    var playerCell : GameObject;
    var goalDoor : GameObject;
    var shortestPathSoFar : float;
    @HideInInspector
    var waitToStart : int = 5;
    var currentMoveSpeed : float = 5;

    var randomizedCourse : boolean = false;
    var randomizeCourseVector : Vector3;
    var calculatedNewRandomizeCourseVector : boolean;

    function Awake ()
    {
    shortestPathSoFar = Mathf.Infinity;
    playerCar = GameObject.FindWithTag("Player").GetComponent(Play erCar);
    playerTransform = GameObject.FindWithTag("Player").transform;
    waitToStart = 5;
    randomizeCourseVector = transform.position;
    }

    function Update ()
    {
    if (waitToStart <= 0)
    {
    playerCell = playerCar.currentCell;
    for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
    {
    for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cel ls.length - 1; i++)
    {
    if (doorCheckingNow.GetComponent(AIpathDoorScript).ce lls == playerCell)
    if (doorCheckingNow.GetComponent(AIpathDoorScript).do orsToCells < shortestPathSoFar) Error Here!
    {
    goalDoor = doorCheckingNow;
    shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doo rsToCells;
    }
    }
    }
    shortestPathSoFar = Mathf.Infinity;
    }
    waitToStart -= 1;

    if (!calculatedNewRandomizeCourseVector)
    {
    randomizeCourseVector = FindRandomSpotInCurrentCell();
    calculatedNewRandomizeCourseVector = true;
    }

    if (goalDoor)
    if (!goalDoor.GetComponent(AIpathDoorScript).doorOpen )
    goalDoor = null;

    if (currentCell != playerCell || playerCell == null)
    {
    if (randomizedCourse)
    transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    if (!randomizedCourse)
    {
    transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
    {
    if (goalDoor)
    randomizedCourse = true;
    if (goalDoor == null)
    calculatedNewRandomizeCourseVector = false;
    }
    }
    }

    if (playerCell == currentCell)
    transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    }

    function OnTriggerEnter (hitTrigger : Collider)
    {
    if (hitTrigger.tag == "AIpathCell")
    {
    currentCell = hitTrigger.gameObject;
    randomizedCourse = false;
    calculatedNewRandomizeCourseVector = false;
    }
    }

    function FindRandomSpotInCurrentCell ()
    {
    return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localSc ale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localSca le.z * -0.5,currentCell.transform.localScale.z * 0.5)));
    }
     
  32. SubjectSix

    SubjectSix

    Joined:
    Mar 12, 2013
    Posts:
    6
    Hey! I love your tutorials, and I am happily enjoying them, but I am getting a really annoying error. I keep getting spammed in the console when I hit play, "NullReferenceException: Object reference not set to an instance of an object
    GunScript.LateUpdate () (at Assets/GunScript.js:70) "
    And I get what ETeeski gets at 51:39 in tutorial 1.23, and I cant fix it, any help?

    GunScript:
    var beingHeld : boolean = false;
    var outsideBox : GameObject;
    @HideInInspector
    var countToThrow : int = -1;
    @HideInInspector
    var playerTransform : Transform;
    @HideInInspector
    var playerMovementScript : PlayerMovementScript;

    var cameraObject : GameObject;
    @HideInInspector
    var targetXRotation : float;
    @HideInInspector
    var targetYRotation : float;
    @HideInInspector
    var targetXRotationV : float;
    @HideInInspector
    var targetYRotationV : float;

    var rotateSpeed : float = 0.3;

    var holdHeight : float = -0.5;
    var holdSide : float = 0.5;
    var racioHipHold : float = 1;
    var hipToAimSpeed : float = 0.1;
    @HideInInspector
    var racioHipHoldV : float;

    var aimRacio : float = 0.4;

    var zoomAngle : float = 30;

    var fireSpeed : float = 15;
    @HideInInspector
    var waitTillNextFire : float = 0;
    var bullet : GameObject;
    var bulletSpawn : GameObject;

    var shootAngleRandomizationAiming : float = 2;
    var shootAngleRandomizationNotAiming : float = 5;

    var recoilAmount : float = 0.5;
    var recoilRecoverTime : float = 0.2;
    @HideInInspector
    var currentRecoilZPos : float;
    @HideInInspector
    var currentRecoilZPosV : float;

    var bulletSound : GameObject;
    var muzzleFlash : GameObject;

    var gunbobAmountX : float = 0.5;
    var gunbobAmountY : float = 0.5;
    var currentGunBobX : float;
    var currentGunBobY : float;

    function Awake ()
    {
    countToThrow = -1;
    playerTransform = GameObject.FindWithTag("Player").transform;
    playerMovementScript = GameObject.FindWithTag("Player").GetComponent(Play erMovementScript);
    cameraObject = GameObject.FindWithTag("MainCamera");
    }

    function LateUpdate ()
    {
    if(beingHeld)
    {
    rigidbody.useGravity = false;
    outsideBox.GetComponent(Collider).enabled = false;

    currentGunBobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScrip t).headbobStepCounter) * gunbobAmountX * racioHipHold;
    currentGunBobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScrip t).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;

    var holdMuzzleFlash : GameObject;
    var holdSound : GameObject;
    if(Input.GetButton("Fire1"))
    {
    if(waitTillNextFire <= 0)
    {
    if(bullet)
    Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    if(bulletSound)
    holdSound = Instantiate(bulletSound,bulletSpawn.transform.posi tion, bulletSpawn.transform.rotation);
    if(muzzleFlash)
    holdmuzzleFlash = Instantiate(muzzleFlash,bulletSpawn.transform.posi tion, bulletSpawn.transform.rotation);
    targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    currentRecoilZPos -= recoilAmount;
    waitTillNextFire = 1;
    }
    }
    waitTillNextFire -= Time.deltaTime * fireSpeed;

    if(holdSound)
    holdSound.transform.parent = transform;
    if(holdmuzzleFlash)
    holdmuzzleFlash.transform.parent = transform;

    currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);

    cameraObject.GetComponent(MouseLookScript).current TargetCameraAngle = zoomAngle;

    if(Input.GetButton("Fire2")){
    cameraObject.GetComponent(MouseLookScript).current AimRacio = aimRacio;
    racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    if(Input.GetButton("Fire2") == false){
    cameraObject.GetComponent(MouseLookScript).current AimRacio = 1;
    racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}

    transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunBobX, holdHeight + currentGunBobY * racioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0, currentRecoilZPos));

    targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotati on, targetXRotationV, rotateSpeed);
    targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotati on, targetYRotationV, rotateSpeed);

    transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    }
    if(!beingHeld)
    {
    rigidbody.useGravity = true;
    outsideBox.GetComponent(Collider).enabled = true;

    countToThrow -= 1;
    if (countToThrow == 0)
    rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);

    if(Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun Input.GetButtonDown("Use Key") playerMovementScript.waitFrameForSwitchGuns <= 0)
    {
    playerMovementScript.currentGun.GetComponent(GunSc ript).beingHeld = false;
    playerMovementScript.currentGun.GetComponent(GunSc ript).countToThrow = 2;
    playerMovementScript.currentGun = gameObject;
    beingHeld = true;
    targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotati on - 180;
    playerMovementScript.waitFrameForSwitchGuns = 2;
    }
    }
    }

    Player Movement Script:
    var currentGun : GameObject;
    var distToPickUpGun : float = 6;
    var throwGunUpForce : float = 100;
    var throwGunForwardForce : float = 300;
    var waitFrameForSwitchGuns : int = -1;

    var walkAcceleration : float = 5;
    var walkAccelAirRacio : float = 0.1;
    var walkDeacceleration : float = 5;
    @HideInInspector
    var walkDeaccelerationVolx : float;
    @HideInInspector
    var walkDeaccelerationVolz : float;

    var cameraObject : GameObject;
    var maxWalkSpeed : float = 20;
    @HideInInspector
    var horizontalMovement : Vector2;

    var jumpVelocity : float = 20;
    @HideInInspector
    var grounded : boolean = false;
    var maxSlope : float = 60;

    var crouchRacio : float = 0.3;
    var transitionToCrouchSec : float = 0.2;
    var crouchingVelcoity : float;
    var currentCrouchRacio : float = 1;
    var originalLocalScaleY : float;
    var crouchLocalScaleY : float;
    var collisionDetectionSphere : GameObject;

    function Awake ()
    {
    currentCrouchRacio = 1;
    originalLocalScaleY = transform.localScale.y;
    crouchLocalScaleY = transform.localScale.y * crouchRacio;
    }

    function LateUpdate ()
    {
    waitFrameForSwitchGuns -= 1;

    transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    if (Input.GetButton("Crouch"))
    currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelcoity, transitionToCrouchSec);
    if(Input.GetButton("Crouch") == false collisionDetectionSphere.GetComponent(CollisionDec ectionSphereScript).collisionDetected == false)
    currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelcoity, transitionToCrouchSec);

    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;

    if(grounded){
    rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}

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

    if(grounded)
    rigidbody.AddRelativeForce(Input.GetAxis("Horizont al") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    else
    rigidbody.AddRelativeForce(Input.GetAxis("Horizont al") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);

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

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

    function OnCollisionExit ()
    {
    grounded = false;
    }
     
  33. Ragegod5

    Ragegod5

    Joined:
    Jul 23, 2013
    Posts:
    2
  34. SquidHorse

    SquidHorse

    Joined:
    May 26, 2013
    Posts:
    11
    FPS 1.29

    NullReferenceException: Object reference not set to an instance of an object

    I am getting this error whenever I walk into the sphere collider for the ammo crate or gun object to try and pick up ammo. I noticed several people have the same problem under comments in that video but nobody has been able to offer a solution. This has been driving me crazy and I realize the error is saying it cannot find the object we are trying to reference but it does not make sense as I have attached the game objects and script wherever possible. I am not going to attach my code for this because it is literally identical to the code in the tutorial and I know that is not where the issue is stemming from. Would really appreciate any help that you could offer me.

    P.S. - absolutely freaking love your tutorials and have literally learned more from you in the past few weeks than an entire year of school has been able to teach me, I am forever indebted!!
     
  35. SquidHorse

    SquidHorse

    Joined:
    May 26, 2013
    Posts:
    11
    Just figured it out, I had my AmmoPickupScript attached to my pistol game object and not the ammo pickup sphere, thanks again for the tuts Eteeski keep doin what you're doin man!
     
  36. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
  37. TKBaha

    TKBaha

    Joined:
    Sep 2, 2013
    Posts:
    3
  38. Kritos01

    Kritos01

    Joined:
    Sep 14, 2013
    Posts:
    1
    MY gun Just Dissepears (sorry the spelling) its just gone when i hit play Why?
     
  39. vikass_ext

    vikass_ext

    Joined:
    Jan 30, 2013
    Posts:
    1
    I have a problem in my enemy collision. In my scene enemy cross all the game objects like cars buildings etc. please help me to fix this issue.
     
  40. Nmovies11

    Nmovies11

    Joined:
    Oct 20, 2013
    Posts:
    1
    FPS1.4 Setting a Max Walk Speed.
    Hey i am having problems with my script because it followed everything you did but i got a error in line 12 saying this : expecting :, found '='.
    script :
    var walkAcceleration : float = 5;
    var cameraObject : GameObject;
    var maxWalkSpeed : float = 20;
    @HideInInspector
    var horizontalMovement : Vector2;

    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);
    }
     
    Last edited: Oct 20, 2013
  41. Tomdancer

    Tomdancer

    Joined:
    Oct 20, 2013
    Posts:
    2
    Hello there ETeeski !

    I have a little problem with the EnemyMovementScript in Fps 1.38. I did everything exactly as you did, but my little spheres are not rotating. I checked my Script trough your Script reference page, copy pasted the ones i needed for rotation but nothing changed :/ I will be so happy if you could help me with this problem.
    Keep making tutorials, they really help a lot. Oh and sorry for my English i'm from Hungary :)

    Code (csharp):
    1. var currentCell : GameObject;
    2. var playerMovementScript : PlayerMovementScript;
    3. var playerTransform : Transform;
    4. var playerCell : GameObject;
    5. var goalDoor : GameObject;
    6. var shortestPathSoFar : float;
    7. @HideInInspector
    8. var waitToStart : int = 5;
    9. var maxMoveSpeed : float = 5;
    10. var minMoveSpeed : float = 1;
    11. var speedDamage : float = 0.5;
    12. var speedRecover : float = 1;
    13.  
    14. var currentMoveSpeed : float = 5;
    15. var randomizedCourse : boolean = false;
    16. var randomizeCourseVector : Vector3;
    17. var calculatedNewRandomizeCourseVector : boolean;
    18.  
    19. var lastPos : Vector3;
    20.  
    21. function Awake ()
    22. {
    23.     shortestPathSoFar = Mathf.Infinity;
    24.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    25.     playerTransform = GameObject.FindWithTag("Player").transform;
    26.     waitToStart = 5;
    27.     randomizeCourseVector = transform.position;
    28.     lastPos = transform.position;
    29. }
    30. function Update ()
    31. {
    32.     if (waitToStart <= 0)
    33.     {
    34.         playerCell = playerMovementScript.currentCell;
    35.         for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
    36.         {
    37.             for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cells.length - 1; i++)
    38.             {
    39.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).cells[i] == playerCell)
    40.                 if (doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i] < shortestPathSoFar)
    41.                 {
    42.                     goalDoor = doorCheckingNow;
    43.                     shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i];
    44.                 }
    45.             }
    46.         }
    47.         shortestPathSoFar = Mathf.Infinity;
    48.     }
    49.     waitToStart -= 1;
    50.     if (!calculatedNewRandomizeCourseVector)
    51.     {
    52.         randomizeCourseVector = FindRandomSpotInCurrentCell();
    53.         calculatedNewRandomizeCourseVector = true;
    54.     }
    55.     if (goalDoor)
    56.     if (!goalDoor.GetComponent(AIpathDoorScript).doorOpen)
    57.         goalDoor = null;
    58.     if (currentCell != playerCell || playerCell == null)
    59.     {
    60.         if (randomizedCourse)
    61.             transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    62.         if (!randomizedCourse)
    63.         {
    64.             transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    65.             if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
    66.             {
    67.                 if (goalDoor)
    68.                     randomizedCourse = true;
    69.                 if (goalDoor == null)
    70.                     calculatedNewRandomizeCourseVector = false;
    71.             }
    72.         }
    73.     }
    74.     if (playerCell == currentCell)
    75.         transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    76.        
    77.     if (currentMoveSpeed < maxMoveSpeed)
    78.         currentMoveSpeed += speedRecover * Time.deltaTime;
    79.     if (currentMoveSpeed > maxMoveSpeed)
    80.         currentMoveSpeed = maxMoveSpeed;  
    81.        
    82.      transform.rotation = Quaternion.LookRotation(transform.position - lastPos);
    83.  
    84.     lastPos = transform.position;        
    85.                    
    86.                                
    87. }
    88. function OnTriggerEnter (hitTrigger : Collider)
    89. {
    90.     if (hitTrigger.tag == "AIpathCell")
    91.     {
    92.         currentCell = hitTrigger.gameObject;
    93.         randomizedCourse = false;
    94.         calculatedNewRandomizeCourseVector = false;
    95.     }
    96. }
    97.     function OnTriggerStay (hitTrigger : Collider)
    98. {
    99.    if(hitTrigger.tag  == "Enemy"  hitTrigger.gameObject != gameObject)
    100.    {
    101.        if(currentMoveSpeed > minMoveSpeed)
    102.           currentMoveSpeed -= speedDamage;
    103.           transform.position += (transform.position - hitTrigger.transform.position).normalized * 0.1;
    104.    }  
    105. }
    106.  
    107. function FindRandomSpotInCurrentCell ()
    108. {
    109.     return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localScale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localScale.z * -0.5,currentCell.transform.localScale.z * 0.5)));
    110. }
     
  42. Tomdancer

    Tomdancer

    Joined:
    Oct 20, 2013
    Posts:
    2
    Oh well hello again :)
    My problem solved itself it was just some unity bug thing. They were actually rotating but the arrows werent rotated with them.So yeah its working :)
    Keep making tutorials/games:)
     
  43. danish-zia

    danish-zia

    Joined:
    Jul 21, 2013
    Posts:
    27
  44. danish-zia

    danish-zia

    Joined:
    Jul 21, 2013
    Posts:
    27
  45. Galen Altaiir

    Galen Altaiir

    Joined:
    Nov 1, 2013
    Posts:
    2
  46. MisterPotatoHead

    MisterPotatoHead

    Joined:
    Nov 2, 2013
    Posts:
    1
    FPS1.3 problem........

    The capsule doesn't rotate with the camera... Any suggestions?
     
  47. GhostLlamaSniper

    GhostLlamaSniper

    Joined:
    Nov 21, 2013
    Posts:
    1
    do you have any ideas for making a fps game that combines melee and guns, that can be dual wielded? Im not good at scripting, im just a modeler, but im also trying to be a one man wrecking crew at this moment. But those and a weapon wheel are my main concern
     
  48. grebo99

    grebo99

    Joined:
    Dec 6, 2013
    Posts:
    1
    FPS 1.30
    I have a problem when I press "E" on my keyboard my door wont open. I only have the door script in my game and Unity is giving me the error:
    NullReferenceExceptionDoorScript.Awake () (at Assets/Standard Assets/Imported/DoorScript.js:39)

    I am currently running Unity 4.2.1.
     
  49. SteveVerhoeven

    SteveVerhoeven

    Joined:
    Nov 14, 2013
    Posts:
    4
    problem with FPS 1.9
    ---------------------------
    I have a simular problem, when i aim the gun, the sights move a little bit to the center but it doesnt center well ... please help
     
  50. Delusional_Tacos

    Delusional_Tacos

    Joined:
    Jan 8, 2014
    Posts:
    2
    I'm having a little problem with FPS1.8, I created a basic gun instead of using a pre-made model (I have one to test as well)
    The problem is that when you look towards the gun it just goes everywhere around the player capsule. Here's a short video showing what I mean, Sorry about the quality.



    The code is 99.99% identical to the video one

    Code (csharp):
    1. var cameraObject : GameObject;
    2. @HideInInspector
    3. var targetXRotation : float;
    4. @HideInInspector
    5. var targetYRotation : float;
    6. @HideInInspector
    7. var targetXRotationV : float;
    8. @HideInInspector
    9. var targetYRotationV : float;
    10.  
    11. var rotationSpeed : float = 0.3;
    12.  
    13. var holdHeight : float = -0.5;
    14. var holdSide : float = 0.5;
    15. //var holdSideL : float = -0.5;
    16.  
    17. function Update ()
    18. {
    19.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * Vector3(holdSide, holdHeight, 0));
    20.    
    21.     targetXRotation = Mathf.SmoothDamp(targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotati  on, targetXRotation, rotationSpeed);
    22.     targetYRotation = Mathf.SmoothDamp(targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotati  on, targetYRotation, rotationSpeed);
    23.    
    24.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    25. }