Search Unity

FPS Controller Setting up help

Discussion in 'Scripting' started by ddulshan, Jan 10, 2015.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Well, as the Topic says I need my FPS Controller crouch. But I don't know how to begin Scripting this.

    I've seen many topics around, but not the answer I'm looking for.

    My idea to make this Crouch is to shortening the Capsule Collider instead of Shortening the whole thing which I guess is easy. And moving the Camera a little down. But I'm not very familiar with Resizing objects through Scripts.

    And if there's a better way to do it instead if Shortening the Collider please let me know.

    Please help and please dnt give links to videos.
     
  2. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    You're on the right track with it but you need to adjust the camera height also.

    Which scripting language are you the most familiar with?
    That info would help to help you better.
     
  3. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @Nubz : Ah damn I forgot to mention it. I'm much familiar with Java.
     
  4. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Well its been awhile since I've done this but I would shorten the collider and change the camera height because you dont want the collider to be as large as the standing object and be crouching when a projectile hits the standing collider the player dies.

    Another method is you could instance another collider while crouching and destroy the old one then when the player stands back up you could do the same for the standing player, however this could be more resource heavy and could reproduce unintended bugs .
     
  5. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    319
    Last edited: Jan 11, 2015
  6. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @kingcharizard : Yeah, I thought of deleting the current collider and replacing with new, but that's Pure Bullshit (No offence to Pure Bullshit). So I need to know how to do the other way.

    @alucardj : I need to start everything from scratch, so I know what I'm doing. And That's a lot to read and I hate it :confused:.
     
  7. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I think I can convert it to Java. Can you explain what Variable initialPos to timingDelta does?o_O

    Thanks!!! :D
     
  8. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Oh, Lol. I need to write down everything again myself so I know what I'm doing so I won't be like WTF later. :cool:

    And I said "Can you explain what Variable initialPos *to* timingDelta does?" Including the Variables in between them. :D
     
  9. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I can figure out how the script works. I just need a little explanation. (Does that make any sense? Lol:rolleyes:) Anyway thanks! :D:D

    Nice profile pic Btw! Lol. o_O

    Edit: @Nubz :Converted to Java, works fine, made some modifications and Thanks!!!
     
    Last edited: Jan 11, 2015
  10. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    I did this a good while ago. Generally, there's two approaches.

    The first one is via scripting. You use Lerp on the collider height. However, since the collider's height is relative to its origin (center), you'll lose ground contact and fall to the floor all the time. Reducing the height by 1, "removes" 0.5 from the top and 0.5 from the bottom.
    You'll have to compensate for that, or your landing sound will cause problems.

    It's possible. In fact, I did it like this when writing the first iteration of my character controller.
    However, it inflates your code and is rather inflexible.

    A much cleaner approach is to take advantage of Mecanim for things like crouching and leaning.
    Less code, more flexibility. You simply have a default state that has no changes and a crouched state where they height and position have been altered accordingly.
    Then you just trigger the state change from within your code.
    If you use a dedicated transition state, you can even alter how exactly your player crouches/stands up.
    If you use a blendtree you can even control how much the player crouches down.
     
  11. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : Well, I'm really bad at Mecanim. TBH I don't even know what it really is! :confused:
    Well, this is my current script and it works almost fine. I guess you can point out to where it needs to be modified to make it more flexible with things.

    Code (JavaScript):
    1. var isStanding : boolean;
    2. var isCrouching : boolean;
    3. var isSprinting : boolean;
    4.  
    5. var myController : CharacterController;
    6. var myCam : Transform;
    7.  
    8. var initialPos : Vector3;
    9. var initialCenterPos : Vector3;
    10. var crouchCenterPos : Vector3;
    11.  
    12. var initialHeight : float;
    13. var crouchingHeight : float;
    14. var timingDelta : float = 0.0f;
    15.  
    16. function Awake ()
    17. {
    18.     myController = GetComponent(CharacterController);
    19.     initialHeight = myController.height;
    20.     initialCenterPos = myController.center;
    21.  
    22.     crouchingHeight = initialHeight - 1.5f;
    23.     crouchCenterPos = initialCenterPos + Vector3.down * 0.75;
    24.  
    25.     isStanding = true;
    26.     isCrouching = false;
    27. }
    28.  
    29. function Update ()
    30. {
    31.     if(Input.GetButtonDown("Crouch") && isStanding == true)
    32.     {
    33.         myController.height = crouchingHeight;
    34.         myController.center = crouchCenterPos;
    35.         Crouch();
    36.     }
    37.  
    38.     else if(Input.GetButtonDown("Crouch") && isCrouching == true)
    39.     {
    40.         myController.height = initialHeight;
    41.         myController.center = initialCenterPos;
    42.         Stand();
    43.     }
    44.  
    45.     if(Input.GetButton("Sprint") && isSprinting == false)
    46.     {
    47.         myController.height = initialHeight;
    48.         myController.center = initialCenterPos;
    49.         Sprint();
    50.         gameObject.SendMessage("FastBreath", SendMessageOptions.DontRequireReceiver);
    51.     }
    52.  
    53.     if(Input.GetButtonUp("Sprint") && isSprinting == true)
    54.     {
    55.         myController.height = initialHeight;
    56.         myController.center = initialCenterPos;
    57.         gameObject.SendMessage("SFastBreath", SendMessageOptions.DontRequireReceiver);
    58.         Stand();
    59.     }
    60. }
    61.  
    62. function Stand ()
    63. {
    64.     myCam.transform.localPosition = Vector3(0f, 0.7f, 0f);
    65.     isStanding = true;
    66.     isCrouching = false;
    67.     isSprinting = false;
    68.     gameObject.SendMessage("StandSpeed", SendMessageOptions.DontRequireReceiver);
    69. }
    70.  
    71. function Crouch ()
    72. {
    73.     myCam.transform.localPosition = Vector3(0f, -0.15f, 0f);
    74.     isStanding = false;
    75.     isCrouching = true;
    76.     gameObject.SendMessage("CrouchSpeed", SendMessageOptions.DontRequireReceiver);
    77. }
    78.  
    79. function Sprint ()
    80. {
    81.     myCam.transform.localPosition = Vector3(0f, 0.7f, 0f);
    82.     isStanding = true;
    83.     isCrouching = false;
    84.     isSprinting = true;
    85.     gameObject.SendMessage("Sprinting", SendMessageOptions.DontRequireReceiver);
    86. }
    Edit : Well got another problem. I've assinged shift button as Sprint button. In another script with Sprinting function I've added to play a sound when Sprinting. But when I just press the Sprint button the sound plays, it's not even running. So how to make the sound play if only the Player is "Sprinting" not just when the sprint button is pressed. Is there a way to assign 2 buttons in the Input section like "left shift + Vertical" or something like?
     
    Last edited: Jan 12, 2015
  12. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Mecanim is Unity's "new" animation system. You should learn it, since it will be extremely useful in many situations (new GUI system for example). Tutorials can be found here.

    After watching the tutorials 1, 2, 9 and 10, you should be able to easily animate the height and position components of your collider and player.

    Give me a few minutes to illustrate the issue...
     
  13. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ahh damn! Videos? My connection is really bad, but I'll try.
     
  14. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Crouching.png

    If you have problems with the Macanim approach, I can give you a sample prefab that shrinks when you press C.
     
    shkar-noori likes this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,861
    You could also break this into two parts: collision and camera.

    For the collision, have the normal collider and the short collider both in the prefab but on separate game objects. Only turn one of them on at a time (using .SetActive()). Essentially when you crouch you would instantaneously become shorter and be able to fit under things.

    For the camera, you would have the script tween down the camera to the new lower height, and tween it back up when you're done crouching.

    I have played XBox games that do precisely this type of crouching, at least judging by how it feels to try and go underneath low-hanging objects when you press against them and tap crouch.

    Speaking of that, if you are going to enable going under things, make sure you check overhead before you allow the player to uncrouch.
     
  16. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : Lol, so the "Part I Want" is done using Mechanim? And yeah please give me sample so I can see how it works. :D

    @Kurt Dekker : Well, as I remember I can't add 2 colliders to the same object. And yeah the Player gets to crouch under vents and stuff. How do check overhead before standing??? I was gonna ask that. ;)
     
  17. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Guys. Please reply. How to check over head before standing again?

    @TheSniperFan : Please give a sample asset.
     
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,861
    To check overhead, you could have a simple trigger object that is always above your head (parented into your player's structure on a separate game object), and when you are crouched, you start to use it to check if the area above you is clear and you can stand up.
     
    ddulshan likes this.
  19. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    You mean something like, if any collider is touching the trigger that is over the player. It can't stand. Yeah get it, thanks!
     
  20. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Use a Capsule- or SphereCast for that.
    I'll quickly make an example using Mecanim for you and upload it here. I wrote a math exam yesterday, so I had no time for Unity stuff the last days.



    EDIT:
    Here you go.
    I included a test scene. If you press C, the capsule will play a crouching animation. If you press C again, the capsule will stand up, if there's enough clearance above. Try crouching and then moving the capsule underneath the second box. It won't stand up unless you move the box high enough.

    I added quite a bit of documentation to the code, so you can understand what is happening. Besides that, there's some Debugging stuff, like a blue ray being drawn whenever the player can stand up, and a red one whenever he can't.

    If you were to remove all this stuff, you'll notice how Mecanim enables you to do all this with an incredibly low amount of code.
    The entire logic is basically 6 lines of code. This includes control structures like checking whether the button was pressed and caching the hash of the crouching state.
     

    Attached Files:

    Last edited: Jan 16, 2015
  21. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : Wow thanks. I'll check this out tomorrow and reply.
     
  22. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : I can't open the file. Error shows up "Error in decompressing pack". And I'm guessing that I need the whole asset file, right?
     
  23. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    That's weird. :confused:
    Maybe something went wrong when uploading the file. I'll check this once I get back from University.
     
  24. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : xD. And I think I'm getting the Hang of Mechanim. So what your telling is "Make a anim that make the collider and camera etc moving down and and a Bool parameter in the Animator and control it using Script?" Well my english may not be perfect but sure you get what I'm trying to say. Lol.
     
  25. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    I'd use a trigger, but yeah, that about sums it up.
    The easiest solution is to have a standard/default/idle state ,which changes nothing, and a crouched state that just has the player fully crouched down. Only the a keyframe right at the beginning/no animation. Mecanim will interpolate between the two all by itself.
     
  26. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok, what about the scripting I'm stuck with it. Java is I'm familiar with. This is what I already did but getting error "'animator' is not a member of 'UnityEngine.Animator'". And also to mention I'm using the Mechanim here to change the Animation of the holding item when the player Sprint, Idle, Run. Currently its only Sprint.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var isStanding : boolean;
    4. var isCrouching : boolean;
    5. var isSprinting : boolean;
    6.  
    7. var myController : CharacterController;
    8. var myCam : Transform;
    9. var currentItem : GameObject;
    10.  
    11. var initialPos : Vector3;
    12. var initialCenterPos : Vector3;
    13. var crouchCenterPos : Vector3;
    14.  
    15. var initialHeight : float;
    16. var crouchingHeight : float;
    17. var timingDelta : float = 0.0f;
    18.  
    19. var torchAnimator : Animator;
    20.  
    21. function Awake ()
    22. {
    23.     myController = GetComponent(CharacterController);
    24.     initialHeight = myController.height;
    25.     initialCenterPos = myController.center;
    26.     currentItem = GameObject.Find("Player/Main Camera/CurrentItem");
    27.  
    28.     crouchingHeight = initialHeight - 1.5f;
    29.     crouchCenterPos = initialCenterPos + Vector3.down * 0.75;
    30.  
    31.     torchAnimator = currentItem.GetComponent(Animator);
    32.  
    33.     isStanding = true;
    34.     isCrouching = false;
    35. }
    36.  
    37. function Update ()
    38. {
    39.     if(Input.GetButtonDown("Crouch") && isStanding == true)
    40.     {
    41.         myController.height = crouchingHeight;
    42.         myController.center = crouchCenterPos;
    43.         Crouch();
    44.     }
    45.  
    46.     else if(Input.GetButtonDown("Crouch") && isCrouching == true)
    47.     {
    48.         myController.height = initialHeight;
    49.         myController.center = initialCenterPos;
    50.         Stand();
    51.     }
    52.  
    53.     if(Input.GetButton("Sprint") && isSprinting == false)
    54.     {
    55.         myController.height = initialHeight;
    56.         myController.center = initialCenterPos;
    57.         Sprint();
    58.     }
    59.  
    60.     if(Input.GetButtonUp("Sprint") && isSprinting == true)
    61.     {
    62.         myController.height = initialHeight;
    63.         myController.center = initialCenterPos;
    64.         Stand();
    65.     }
    66. }
    67.  
    68. function Stand ()
    69. {
    70.     myCam.transform.localPosition = Vector3(0f, 1.2f, 0f);
    71.     isStanding = true;
    72.     isCrouching = false;
    73.     isSprinting = false;
    74.     gameObject.SendMessage("StandSpeed", SendMessageOptions.DontRequireReceiver);
    75. }
    76.  
    77. function Crouch ()
    78. {
    79.     myCam.transform.localPosition = Vector3(0f, -0.15f, 0f);
    80.     isStanding = false;
    81.     isCrouching = true;
    82.     gameObject.SendMessage("CrouchSpeed", SendMessageOptions.DontRequireReceiver);
    83.  
    84. }
    85.  
    86. function Sprint ()
    87. {
    88.     torchAnimator.animator.SetBool("Sprint", true);
    89.     myCam.transform.localPosition = Vector3(0f, 1.2f, 0f);
    90.     isStanding = true;
    91.     isCrouching = false;
    92.     isSprinting = true;
    93.     gameObject.SendMessage("Sprinting", SendMessageOptions.DontRequireReceiver);
    94. }
     
    Last edited: Jan 20, 2015
  27. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Code (csharp):
    1. torchAnimator.animator.SetBool("Sprint",true);
    Must be
    Code (csharp):
    1. torchAnimator.SetBool("Sprint",true);
     
  28. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Thanks. However I was able to figure it since I couldn't come online for some time. Anyway thanks.

    I'm having a problem with player crouch. Well how I did it is scale down animation. But the item that the player is equipped get scaled too since it is a Child of the main player. How do I stop it from scaling?
     
  29. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    You need to use a different hierarchy then. Also, if the player is merely a capsule and no model, why do you scale it? Just change the height and of the collider.
     
  30. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    How to make the height of the collider gradually decrease through animation. Or it won't be any different from the last how I did.
     
  31. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    You just have an animation state with an animation that changes the collider height.
     
  32. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TheSniperFan : Ok. Did It. I'd personally like to thank to you for introducing me into Mechanim. I'm really getting the hang of it and turns out to be real useful and easy! There's only 1 think to do. Making the player cannot stand when crouching inside vents etc.
     
  33. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
  34. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ah. There's a time when Pro Tips cannot be easily understood by Beginners. ;) I'll give it a try anyway. :D
     
  35. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    I reuploaded my example (to Google Drive this time). See if it works for you now.
    Download.
     
    ddulshan likes this.
  36. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Thanks. Is this a better way to see if the player can stand up or using a Trigger over the players head? Because Raycast makes me go nuts, since I'm a beginner I need the simplest best methods.

    This is my current Script, and I know there are many unwanted parts in the script and other easy way to do it but I did this on my own using things I already know. (The current item and Item animator is used for animating the equipped item while running crouching and stuff).

    And also I got another problem. How to see if the player pressed Horizontal or Vertical buttons? I mean I need to play a specific animation while running, I know I can write the Input....("Horizontal") and Input...("Vertical") 2 times. But is there a easy way to do it? Like "Input(GetButtonDown("Horizontal" or(LOL) "Vertical")) ... then play Anim"? I need something like this for making player Sprint too, when pressed Sprint Button and Vertical Button both instead Sprint only.

    Code (JavaScript):
    1. var isStanding : boolean;
    2. var isCrouching : boolean;
    3. var isSprinting : boolean;
    4. var isWalking : boolean;
    5. var isIdle : boolean;
    6.  
    7. var playerAnimator : Animator;
    8. var itemAnimator : Animator;
    9.  
    10. var currentItem : GameObject;
    11.  
    12. function Start ()
    13. {
    14.     isCrouching = false;
    15.     isStanding = true;
    16.     isSprinting = false;
    17.     isWalking = false;
    18.     isIdle = true;
    19.    
    20.     playerAnimator = GetComponent(Animator);
    21.     currentItem = GameObject.Find("Player/Main Camera/CurrentItem");
    22.     itemAnimator = currentItem.GetComponent(Animator);
    23. }
    24.  
    25. function Update ()
    26. {
    27.     if(Input.GetButtonDown("Crouch") && isCrouching == false)
    28.     {
    29.         isCrouching = true;
    30.         isStanding = false;
    31.         Crouch();
    32.     }
    33.     else if(Input.GetButtonDown("Crouch") && isCrouching == true)
    34.     {
    35.         isCrouching = false;
    36.         isStanding = true;
    37.         Stand();
    38.     }
    39.    
    40.     if(Input.GetButton("Sprint") && isSprinting == false && isStanding == true)
    41.     {
    42.         isStanding = true;
    43.         isIdle = false;
    44.         isSprinting = true;
    45.         isWalking = false;
    46.         Sprint();
    47.     }
    48.     if(Input.GetButtonUp("Sprint") && isSprinting == true)
    49.     {
    50.         isStanding = true;
    51.         isSprinting = false;
    52.         isIdle = true;
    53.         SendMessage("WalkSpeed");
    54.         playerAnimator.SetBool("Run", false);
    55.         playerAnimator.SetBool("Idle", true);
    56.     }
    57.    
    58.     if(Input.GetButton("Vertical") && isCrouching == false && isSprinting == false)
    59.     {
    60.         isStanding = true;
    61.         isCrouching = false;
    62.         isWalking = true;
    63.         isIdle = false;
    64.         Walk();
    65.     }
    66.     if(Input.GetButtonUp("Vertical") && isWalking == true)
    67.     {
    68.         isWalking = false;
    69.         isIdle = true;
    70.         Idle();
    71.     }
    72.    
    73.     if(isIdle)
    74.     {
    75.         Idle();
    76.     }
    77. }
    78.  
    79. function Crouch ()
    80. {
    81.     playerAnimator.SetBool("Crouch", true);
    82.     playerAnimator.SetBool("Stand", false);
    83.     SendMessage("CrouchSpeed");
    84. }
    85.  
    86. function Stand ()
    87. {
    88.     playerAnimator.SetBool("Crouch", false);
    89.     playerAnimator.SetBool("Stand", true);
    90.     SendMessage("WalkSpeed");
    91. }
    92.  
    93. function Sprint ()
    94. {
    95.     itemAnimator.SetBool("Idle", false);
    96.     itemAnimator.SetBool("Walk", false);
    97.     itemAnimator.SetBool("Sprint", true);
    98.     playerAnimator.SetBool("Run", true);
    99.     playerAnimator.SetBool("Idle", false);
    100.     SendMessage("Sprinting");
    101. }
    102.  
    103. function Walk ()
    104. {
    105.     itemAnimator.SetBool("Idle", false);
    106.     itemAnimator.SetBool("Walk", true);
    107.     itemAnimator.SetBool("Sprint", false);
    108.     SendMessage("WalkSpeed");
    109. }
    110.  
    111. function Idle ()
    112. {
    113.     itemAnimator.SetBool("Idle", true);
    114.     itemAnimator.SetBool("Walk", false);
    115.     itemAnimator.SetBool("Sprint", false);
    116. }
    Thanks for helping me this far! :D
     
  37. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Raycasts are dead-simple. If you don't get them, you need to learn them, because they're one of the more flexible tools.

    Here's a small illustration I have drawn for you:
    RCSC.png

    A raycast has a starting point (or origin), a direction and a length. What it does is is very simple. Starting from the origin, it 'shoots a ray' given the direction and length and checks whether it hits something.

    A spherecast is basically a raycast with a radius. Imagine that you're placing a sphere at the starting point, move it in the given direction and see whether it hits something on the way.
    That way you can see if something will fit through a hole. When working with sphere- or capsulecasts, don't forget that the origin is not the lowest point of the check. I've highlighted it in red.

    To answer your second question:
    Code (csharp):
    1. // Boolean operators
    2. // x AND y
    3. x && y
    4.  
    5. // x OR y
    6. x || y
    7.  
    8. // NOT x
    9. !x
    10.  
    11. // x XOR y (xor means exclusive or. The 'normal' or means at least one is true, xor means exactly one is true)
    12. x ^ y
    13.  
    14.  
    15. if(Input.GetAxis("Horizontal") =! 0.0f || Input.GetAxis("Vertical") =! 0.0f) {
    16.     // At least one is pressed
    17. }
     
    ddulshan likes this.
  38. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hmm Thanks. I get how Raycast works. I'll look into more Tuts and the Protip ;) on it.

    Got the Bools!
     
  39. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Guess what! I need another help! :D

    I need to play random sounds when player is moving, I've used arrays for storing the sound files and want to know what next. My current code, not much actually...

    Code (JavaScript):
    1. var runWaitTime : float = 0.5;
    2. var sprintWaitTime : float = 0.2;
    3. var sneakWaitTime : float = 1.0;
    4.  
    5. var normalSteps : float[];
    6. var ventSteps : float [];
    7.  
    8. var inVent : boolean;    
     
  40. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Have a look at this thread.
    I don't know whether they changed it, but their old approach was using a sine curve to determine when to play a sound.
     
  41. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Do I need to download that!? That wont be happening! :(
     
  42. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    What's the problem?
     
  43. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    My connection is not good. And real slow!
     
  44. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok I was able to get the Array sounds working. As the sounds are played as foot steps, I need a pause between the sound files. But I can't make it, the sounds keep playing continuously when a move button is pressed. Here's my current code.

    Code (JavaScript):
    1. var runWaitTime : float = 0.5;
    2. var sprintWaitTime : float = 0.2;
    3. var sneakWaitTime : float = 1.0;
    4.  
    5. var normalSteps : AudioClip[];
    6. var ventSteps : AudioClip[];
    7.  
    8. var inVent : boolean;
    9.  
    10. function Update ()
    11. {
    12.     if(Input.GetButton("Vertical") || Input.GetButton("Horizontal") && runWaitTime >= Time.timeScale)
    13.     {
    14.         audio.PlayOneShot(normalSteps[Random.Range(0,normalSteps.Length)]);
    15.     }
    16. }
     
  45. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Ewwwww. JavaScript.

    Pseudocode time!
    Code (csharp):
    1. if (player is walking) {
    2.          increase move_float a bit;
    3.          if (move_float is greater than step_size) {
    4.                    play_sound;
    5.                    set move_float to 0;
    6.          }
    7. }
    You'll need to figure out a good step_size and how exactly you increase move_float.

    See, if you can make something out of this. This forum is not there to code your games for you, you know? ;)
     
  46. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Yeah, its ok I was afraid I'd be told it anytime sooner from the way I was going. :D

    And Glad I learned Pseudo code at school. ;) Also I was able to get Spherecast work!

    Ok. The step size is the Time until the next sound is played right? And the move float? Would it be Time? And how do I play a random sound from the Array??? The last line of the Code would work right?
     
  47. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    move_float is »some value« that is increased by »some amount« whenever you move.
    When move_float reaches step_size, a step was made. So logically step_size must be »some value« that makes sense in proportion.

    You can play a random sound from an array with given length, by choosing the index randomly between 0 and the array size.
     
  48. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Time.time can be used Move_float right?
     
  49. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    686
    Basing this on the time isn't a good idea, since time passes regardless.
    Determine how much you moved instead.
     
  50. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    How much time the move key is pressed?
     
unityunity