Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WaterMinecraft · Dec 06, 2013 at 03:03 AM · slowing-down

Crouch script (How to add slow speed and untoggle)

Just what the title says! I'm looking to make my script untoggeable, and make the speed slower than regular! I will post it up!

 private var crouchHeight : float;
 private var standardHeight : float;
 private var crouching : boolean = false;
 private var controller : CharacterController;
 private var mainCamera : GameObject;
 
 
 function Start () {
     controller = GetComponent(CharacterController);
     mainCamera = gameObject.FindWithTag("MainCamera");
     standardHeight = controller.height;
     crouchHeight = controller.height/2;
     crouching = false;
 }
 
 function Update () {
     if (Input.GetKeyDown (KeyCode.C))
     {    
         if(crouching){
             controller.height = standardHeight ;
             controller.center = Vector3 (0, 0, 0);
             mainCamera.transform.localPosition.y += crouchHeight;
             crouching = false;
             return;
             
             }
 
     if(!crouching)
     crouch();
     }
 }
 
 function crouch() {
         controller.height = crouchHeight;
         controller.center = Vector3 (0, -0.5, 0);
         mainCamera.transform.localPosition.y -= crouchHeight;
         crouching = true;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kiritaku · Dec 06, 2013 at 03:43 AM

i don't normally use javascript and was too lazy to compile it, so sorry if there are syntax errors ;) something like this should work.

 private var crouchKeyWasDown : boolean;
 private var crouching : boolean;
 
 function Start() {
     crouching = false;
     crouchKeyWasDown = false;
 }
 
 function Update() {
     if (!crouchKeyWasDown && Input.GetKeyDown(KeyCode.C)) {
         crouchKeyWasDown = true;
         crouching = !crouching;
         if (crouching) {
             // adjust your speed/height/etc for crouched in here
         } else {
             // adjust your speed/height/etc for standing in here
         }
     } else if (crouching && Input.GetKeyUp(KeyCode.C)) {
         crouchKeyWasDown = false;
     }
 }

also probably at some point it would be better to use Input.GetButton() so you could rebind the crouch key... but get it working the simplest way first :)

just noticed you asked about slowing the character down as well... if you're still not sure how to do it, then show me the code where you make the character move and i can help.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by WaterMinecraft · Dec 06, 2013 at 06:07 AM

Hey, again. I tried adding the script from my CrouchScript (I'm so tired I can't even handle myself. So I'll more than likely fix it after I get sleep) to the FPSenhancedScript. I'll post it up... I'm not quite sure what's wrong. I'll post the error codes as well.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CharacterController))]
 public class FPSwalkerEnhanced: MonoBehaviour {
 
     public float walkSpeed = 6.0f;
 
     public float runSpeed = 11.0f;
 
     public float crouchSpeed = 3.0f;
 
     // If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster
     public bool limitDiagonalSpeed = true;
 
     // If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down and walks otherwise
     // There must be a button set up in the Input Manager called "Run"
     public bool toggleRun = false;
 
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
 
     // Units that player can fall before a falling damage function is run. To disable, type "infinity" in the inspector
     public float fallingDamageThreshold = 10.0f;
 
     // If the player ends up on a slope which is at least the Slope Limit as set on the character controller, then he will slide down
     public bool slideWhenOverSlopeLimit = false;
 
     // If checked and the player is on an object tagged "Slide", he will slide down it regardless of the slope limit
     public bool slideOnTaggedObjects = false;
 
     public float slideSpeed = 12.0f;
 
     // If checked, then the player can change direction while in the air
     public bool airControl = false;
 
     // Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast
     public float antiBumpFactor = .75f;
 
     // Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping
     public int antiBunnyHopFactor = 1;
 
     private Vector3 moveDirection = Vector3.zero;
     private bool grounded = false;
     private CharacterController controller;
     private Transform myTransform;
     private float speed;
     private RaycastHit hit;
     private float fallStartLevel;
     private bool falling;
     private float slideLimit;
     private float rayDistance;
     private Vector3 contactPoint;
     private bool playerControl = false;
     private int jumpTimer;
 
     private bool crouchKeyWasDown;
     private float crouchHeight;
     private float standardHeight;
     private bool crouching = false;
     private GameObject mainCamera;
 
 
 
     private float height;
 
     void Start() {
         controller = GetComponent<CharacterController>();
         myTransform = transform;
         speed = walkSpeed;
         rayDistance = controller.height * .5f + controller.radius;
         slideLimit = controller.slopeLimit - .1f;
         jumpTimer = antiBunnyHopFactor;
         height = controller.height;
     }
 
     void FixedUpdate() {
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
         float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && limitDiagonalSpeed)? .7071f : 1.0f;
 
         if (grounded) {
             bool sliding = false;
             // See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
             // because that interferes with step climbing amongst other annoyances
             if (Physics.Raycast(myTransform.position, -Vector3.up, out hit, rayDistance)) {
                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                     sliding = true;
             }
             // However, just raycasting straight down from the center can fail when on steep slopes
             // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
             else {
                 Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out hit);
                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                     sliding = true;
             }
 
             // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
             if (falling) {
                 falling = false;
                 if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
                     FallingDamageAlert (fallStartLevel - myTransform.position.y);
             }
 
             // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
             if (!toggleRun)
                 speed = Input.GetButton("Run")? runSpeed : walkSpeed;
 
             // If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on
             if ( (sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && hit.collider.tag == "Slide") ) {
                 Vector3 hitNormal = hit.normal;
                 moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
                 Vector3.OrthoNormalize (ref hitNormal, ref moveDirection);
                 moveDirection *= slideSpeed;
                 playerControl = false;
             }
             // Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
             else {
                 moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
                 moveDirection = myTransform.TransformDirection(moveDirection) * speed;
                 playerControl = true;
             }
 
             // Jump! But only if the jump button has been released and player has been grounded for a given number of frames
             if (!Input.GetButton("Jump"))
                 jumpTimer++;
             else if (jumpTimer >= antiBunnyHopFactor) {
                 moveDirection.y = jumpSpeed;
                 jumpTimer = 0;
             }
         }
         else {
             // If we stepped over a cliff or something, set the height at which we started falling
             if (!falling) {
                 falling = true;
                 fallStartLevel = myTransform.position.y;
             }
 
             // If air control is allowed, check movement but don't touch the y component
             if (airControl && playerControl) {
                 moveDirection.x = inputX * speed * inputModifyFactor;
                 moveDirection.z = inputY * speed * inputModifyFactor;
                 moveDirection = myTransform.TransformDirection(moveDirection);
             }
         }
 
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
 
         // Move the controller, and set grounded true or false depending on whether we're standing on something
         grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
 
         
         // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
         if(!crouchKeyWasDown && Input.GetKeyDown(KeyCode.LeftControl)){
 
 
             crouchKeyWasDown = true;
             crouching = !crouching;
             controller.height = crouchHeight;
             controller.center = Vector3 (0, -0.5, 0);
             mainCamera.transform.localPosition.y -= crouchHeight;
             crouching = true;
         } else{
             controller.height = standardHeight ;
             controller.center = Vector3 (0, 0, 0);
             mainCamera.transform.localPosition.y += crouchHeight;
             crouching = false;
 
         } if (crouching && Input.GetKeyUp(KeyCode.LeftControl)) {
             crouchKeyWasDown = false;
         }
     }
 
     void Update () {
 
 
         // If the run button is set to toggle, then switch between walk/run speed. (We use Update for this...
         // FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
         if (toggleRun && grounded && Input.GetButtonDown("Run"))
             speed = (speed == walkSpeed? runSpeed : walkSpeed);
     }
 
     // Store point that we're in contact with for use in FixedUpdate if needed
     void OnControllerColliderHit (ControllerColliderHit hit) {
         contactPoint = hit.point;
     }
 
     // If falling damage occured, this is the place to do something about it. You can make the player
     // have hitpoints and remove some of them based on the distance fallen, add sound effects, etc.
     void FallingDamageAlert (float fallDistance) {
         print ("Ouch! Fell " + fallDistance + " units!");   
     }
 }




Assets/_Scripts/FPSwalkerEnhanced.cs(162,45): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Assets/_Scripts/FPSwalkerEnhanced.cs(163,46): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable Assets/_Scripts/FPSwalkerEnhanced.cs(167,45): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected Assets/_Scripts/FPSwalkerEnhanced.cs(168,46): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable

I think, after looking it over, I need to set transform to a variable and use that variable..

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kiritaku · Dec 06, 2013 at 04:34 PM 0
Share

ah ok. i updated my original answer. check line 15-16. should help you stand back up :)

for the speed adjustment, you should adjust the part that is

// If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down if (!toggleRun) speed = Input.GetButton("Run")? runSpeed : walkSpeed;

and change it to something like

if (crouching) { speed = crouchSpeed; } else if (!toggleRun) { speed = Input.GetButton("Run")? runSpeed : walkSpeed; }

avatar image WaterMinecraft · Dec 06, 2013 at 06:38 PM 0
Share

I got an error. d: I just edited the top one with the new script. d:

avatar image kiritaku · Dec 06, 2013 at 09:28 PM 0
Share

kk ill check it when I get home. like I said, I was too lazy to compile it :)

avatar image kiritaku · Dec 06, 2013 at 10:27 PM 0
Share

not home yet but the first script you showed me was in javascript and the second one is in c#. the errors youre getting are most likely code for one language getting compiled as if it was the other one :)

avatar image WaterMinecraft · Dec 09, 2013 at 03:32 AM 0
Share
  controller.center = Vector3 (0, -0.5, 0);
          mainCamera.transform.localPosition.y -= crouchHeight;
          crouching = true;
        } else{
          controller.height = standardHeight ;
          controller.center = Vector3 (0, 0, 0);
          mainCamera.transform.localPosition.y += crouchHeight;

It looks like it is getting an error at the Vector3's?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NavMeshAgents with same variables acting differently 1 Answer

How to increase a slowing down speed? 1 Answer

Cooldown button system 1 Answer

Android build slows time after a while 0 Answers

AI Bogging Down Game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges