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 thornekey · Jan 01, 2014 at 08:56 AM · javascriptfpscrouch

force player to crouch when under an object with collisio

Ok so i have a crouching script to which i followed from a tutorial. But what i want to do is that when the player is crouching, if they try to uncrouch and there is an object with collision on them top of them theyre forced to stay crouched.. if you get what i mean.. any help would be greatly apreciated.

 private var crouchHeight : float;
 private var standardHeight : float;
 private var crouching : boolean = false;
 private var controller : CharacterController;
 private var mainCamera : GameObject;
 var GameObj : GameObject;
  
 
 
 
 function Start () {
 
 controller = GetComponent(CharacterController);
 mainCamera = gameObject.FindWithTag("MainCamera");
 standardHeight = controller.height;
 crouchHeight = controller.height/2;
 crouching = false;
 }
 
 function Update () {
 if (Input.GetButtonDown ("Crouch")){
 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
2
Best Answer

Answer by jorjdboss · Jan 06, 2014 at 10:44 AM

This is just to add the sparkzbarca's answer but being more specific with implementation. This is untested code so please edit this answer in case of any corrections.

 function Update ()
 {
  if (Input.GetButtonDown ("Crouch"))
  {
   if(crouching)
   {
   // Assuming that the pivot of the character is always at the center, raycasting from the edge of the top of your controller upwards till max height
   // This ensures that the character can only stand up when there is more clearance than the standardheight
   var startPos = transform.position + new Vector3( 0, crouchHeight - (standardHeight *0.5f), 0);
   var length =  (standardHeight - crouchHeight);
   // Because you raycast from inside the capsule collider, it wont detect the character's collider
   if( !Physics.Raycast( startPos , Vector3.up, length) )
     unCrouch();
   }
   else
   {
    crouch();
   }
  }
 }
 
 function unCrouch()
 {
   controller.height = standardHeight ;
   controller.center = Vector3 (0, 0, 0);
   mainCamera.transform.localPosition.y += crouchHeight;
   crouching = false;
 }
  
 function crouch()
 {
   controller.height = crouchHeight;
   controller.center = Vector3 (0, -0.5, 0);
   mainCamera.transform.localPosition.y -= crouchHeight;
   crouching = true;
 }
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 thornekey · Jan 08, 2014 at 11:11 AM 0
Share

thanks heaps

avatar image
0

Answer by sparkzbarca · Jan 01, 2014 at 09:34 AM

 if(input.getbutton("tries to uncrouch button"))
 {
 //raycast up
 distance = vector3.distance (player.transform, raycasthit.point);
 
 if distance is really small
 dont actually call the stand script otherwise do
 }
 
 alternatly
 
 on collsion enter
 {
 if crouched
 bool UncrouchAllowed = false;
 
 }
 
 if(input.getbuttondown("button to uncrouch") && UncrouchAllowed)
 //uncrouch

take your pick

Comment
Add comment · Show 5 · 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 thornekey · Jan 01, 2014 at 09:37 AM 0
Share

thanks but thats not coded :(

avatar image sparkzbarca · Jan 01, 2014 at 09:41 AM 0
Share

the first one to me seems more robust, just check to see if there is something above you, the thing is you shuold just know how far down crouching moves your player

so lets say your transform was at 1 and you crouch and its Y goes to .5

well you need to account for the body and the head so your model is 2 which makes sense for having its center Y at 1

so that means if your model needs the contact point to be at least 1.5 higher than the transform so you cast up if there is nothing in 1.5 meters you allow themt o stand

you can actually short circuit the distance check i just realized.

just do

clearanceHeight = 1.5f;

if(physics.linecast(player.transform.start, player.transform.start + player.transform.up * clearanceHeight)

right away you know if you hit anything at all then you cant stand.

you dont even need to store what you hit or anything, just cast and if you hit you go ok dont if you didnt ok stand.

avatar image sparkzbarca · Jan 01, 2014 at 09:43 AM 0
Share

wait a $$anonymous$$ute are you saying you dont know how to raycast or what?

I mean im not generally willing to literally write the code for you, it's just not helpful. If I do that you'd just be back in a $$anonymous$$ute wanting me to write the next bit of code, if you can't code you need to practice and learn. This is an easy bit to script and I wrote the pseduo code out for you.

avatar image thornekey · Jan 06, 2014 at 09:45 AM 0
Share

bump???????

avatar image jorjdboss · Jan 06, 2014 at 10:57 AM 0
Share

I wrote an implementation in code. if you can't figure it out, first look at the script reference for Physics.Raycast().


Inside update try not using return like the way you used, because if you put some code that's not related to crouching after this block, it will be skipped when crouch key is pressed.

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

20 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 avatar image avatar image avatar image

Related Questions

How do you make a enemy AI hear approaching footsteps? 2 Answers

Problem with Javascript updating variables. 1 Answer

Weapon Switch Animations 1 Answer

fps shooting in the direction of character main cam 1 Answer

How To Make The FPS Character Crouch 8 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