Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 5.4 Crouch Script (free)

Discussion in 'Scripting' started by Spyrox, Oct 28, 2016.

  1. Spyrox

    Spyrox

    Joined:
    Oct 28, 2016
    Posts:
    2
    Hello. I am making a horror game that uses a main function of a crouch to hide from the monsters (spooky), but I am unable to find anything anywhere for a free script for crouching not only the camera, but the FPScontroller hitbox too.

    Mind, I have Zero knowledge of scripting and the most I can do is edit pre-made scripts. I have found tutorials of crouch scripts but they are all outdated and do not work on current version of unity. I did see a script pack with a crouch function for $10, but I am not serious enough to pay money for crouch control.

    This is what I have currently, which will not let me run the game since it has too many compiled script errors.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Crouch2 : MonoBehaviour {
    5.  
    6.     var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;
    7.  
    8. function Start()
    9.     {
    10.         Screen.showCursor = false;
    11.     }
    12.  
    13.  
    14.  
    15.  
    16.     function Update()
    17.     {
    18.  
    19.         if (tongle == 0)
    20.         {
    21.             if
    22. (Input.GetKeyDown
    23. (KeyCode.LeftControl))
    24.             {
    25.  
    26.  
    27.  
    28.  
    29.                 transform.localScale +=
    30.                 Vector3(0.0, -1, 0); tongle = 1;
    31.             }
    32.             return;
    33.         }
    34.  
    35.  
    36.         if (tongle == 1)
    37.         {
    38.             if
    39. (Input.GetKeyDown
    40. (KeyCode.LeftControl))
    41.             {
    42.                 transform.position.y += 0.5;
    43.                 transform.localScale +=
    44.                 Vector3(0.0, 1, 0); tongle = 0;
    45.             }
    46.             return;
    47.         }
    48.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Here's something I put together back when the new assets were Unity 4.6 beta. Not sure how much the controller changed in 5, but the principles should still be the same : http://answers.unity3d.com/questions/733566/crouching-with-new-beta-assets.html

    scroll down to see a list of all the changes that have been made to the script.

    edit: mgear is correct, having understanding of fundamentals will help you advance your journey in coding much faster and less frustrating than just hacking and slashing random code snippets. Hopefully the explanations in that answer also help to understand what is happening in the modifications implemented.
     
    Last edited: Oct 28, 2016
  4. Spyrox

    Spyrox

    Joined:
    Oct 28, 2016
    Posts:
    2
    I plan on using C#, since a lot of people I know at school that are into computers find it a little easier than java, but java is the most widely used.


    This first project is like a test for myself, if I can do what I intend to do with my ideas and that I am capable of it in the time I have free. Else I would just have to commission game ideas and I would Hate to do that. :confused:
    I appreciate the script; as if it wasn't to work it would still teach me a little, along with the tutorials for scripting and would be a good starting point for other custom scripts.

    Right now im busy with WebDesign at school and I wouldn't be surprised if there were a few similarities in coding.