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 victorlarsson · Apr 10, 2012 at 03:39 PM · positionvector3crouch

crouch script

hi, i am making a game and have no idea what im doing wrong here, i am trying to make a script to crouch the character (fps), im using the physicswalker and have a spere with a colider on the grond that is "the feet" that is a child of the player so what i want to do is to have a button that makes the distance between the sphere and the player have a lower value, when i push the button, the position doesnt decrease, here is my script (the value is right if i dont have the if(Input.GetKey("r")) in the script, but then it stays like that all time obviously)

function Start () { if(Input.GetKey("r")) { transform.localPosition = Vector3(0, -0.5, 0); } }

what am i doing wrong?

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

4 Replies

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

Answer by Kryptos · Apr 10, 2012 at 03:59 PM

Start() is only called once (the first time the object is activated, according to the documentation).

You should do this in Update() which is called every frame. Something like:

 function Update()
 {
     if(Input.GetKey("r"))
     {
         transform.localPosition = Vector3(0, -0.5, 0);
     }
     else
     {
         transform.localPosition = Vector3(0, 0, 0);
     }
 }


Comment
Add comment · Show 1 · 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 victorlarsson · Apr 10, 2012 at 06:37 PM 0
Share

thank you very much, i still have a problem though, it worked to go down but when the char were about to rise again the collider teleported through the floor, i guess i would have to make it rise slow but have no idea how to do that, any ideas?

avatar image
1

Answer by Skotous · Aug 04, 2012 at 12:03 PM

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

  1. var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

    function Start () { Screen.showCursor = false; }

    function Update () {

    if (tongle == 0){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.localScale += Vector3(0.0,-1,0); tongle = 1;
    } return; }

    if (tongle == 1){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.position.y += 0.5;
    transform.localScale += Vector3(0.0,1,0); tongle = 0;
    } return; } }

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
1

Answer by Skotous · Aug 04, 2012 at 12:03 PM

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

  1. var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

    function Start () { Screen.showCursor = false; }

    function Update () {

    if (tongle == 0){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.localScale += Vector3(0.0,-1,0); tongle = 1;
    } return; }

    if (tongle == 1){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.position.y += 0.5;
    transform.localScale += Vector3(0.0,1,0); tongle = 0;
    } return; } }

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 Skotous · Aug 04, 2012 at 12:02 PM

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

 function Start () {
 Screen.showCursor = false; }
 
 
 
 
 
 function Update () {
 
 if (tongle == 0){ if
 (Input.GetKeyDown
 (KeyCode.LeftControl)) {    
 transform.localScale +=
 Vector3(0.0,-1,0);    tongle = 1;   
 }    return; }
 
 
 if (tongle == 1){ if
 (Input.GetKeyDown
 (KeyCode.LeftControl)) {   
 transform.position.y += 0.5;   
 transform.localScale +=
 Vector3(0.0,1,0);    tongle = 0;   
 }    return; }

}

P.S. sorry if i spammed that i wasn't sure if i did post it right

Comment
Add comment · Show 2 · 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 BluEye · Sep 03, 2012 at 06:32 AM 0
Share

@Skotous this is my script and I too am having the falling through the floor issue after releasing my crouch button "c" I am unable to use your script. I get all kinds of errors. I am using the Character$$anonymous$$otor.js and FPSInputController.js also.

function Update () {

if (Input.Get$$anonymous$$eyDown ("c")) { transform.position.y -= 0.5;

   transform.localScale.y = 0.25;

}

if (Input.Get$$anonymous$$eyUp ("c")) {

   transform.position.y += .7;

   transform.localScale.y = 2.0;

} }

avatar image Igorotak · Nov 14, 2017 at 06:35 AM 0
Share

How do you fix it if you want the crouch to not work in certain areas (without use of triggers).

example. if youre inside an air vent. if you press the crouch button in areas like this, the character just pops out of the walls. How to fix that?

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

9 People are following this question.

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

Related Questions

Vector3 not receiving coordinates 1 Answer

addition of vector3 1 Answer

Creating a bouncing game without using physics - Vector3 math problem, 2 Answers

How to keep the same position of the object instead of camera 0 Answers

Distance won't work 1 Answer


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