Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dog-Gamer · May 04, 2016 at 10:56 PM · javascriptprogramming

Have FPS Controller Crouch to hide player?

So im making a horror game where the enemy hunts down the player with the tag Player of course. But once they are near I want the player to run and hide under a table or under something that has the tag "Hide" and then they can walk out and they unrouch. I want to use the premade FPS cam also. I also want it to lower the cam to make it look like it is crouhed and also have the box collider shorten along once hidden the grow once not. Thank for the future also. Any help will be appreticated. (prefered lang.: Java Script)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ClearRoseOfWar · May 05, 2016 at 05:34 PM

Well I dont program in java, but I think all you'd need to do is:

in your firstpersoncontroller.cs file

get access to the firstpersoncamera gameobject's transform.

 public Transform cam; //drag firstpersoncamera into inspector.

in the update loop, with a boolean you can use a button press like this:

 bool crouch = false;
 if(input.GetKeyDown(KeyCode.LeftControl))
 crouch = !crouch;

The above will trigger your crouch bool on and off.

then you can do another check in the update to see if crouching or not.

 if(crouching)
 else

Then youll need to add the camera movement. you can lerp between points to make the crouch smooth.

youll need a float to mark where the camera should be lerpped at.

 float _lerpPoint = 0;

then you need another 2 vector locations to lerp to, and start from.

 Vector3 standPos;
 Vector3 crouchPos;

in the start method you can assign the location of the crouch and stand pos;

     crouchPos = new Vector3(cam.transform.position.x, cam.transform.position.y - 1.5f, cam.transform.position.z);// set 1.5 to crouch distance(untested)
 standPos = cam.transform.position;

then you can continue your if statement in the update like so:

  if(crouching && _lerpPoint <1)
 _lerpPoint+= 25 * Time.deltaTime;
  else if(!crouching && _lerpPoint > 0)
 _lerpPoint-= 25 * Time.deltaTime;

and just under that you add the lerp like so:

 cam.transform.position.y = Mathf.Lerp( standPos, crouchPos, _lerpPoint);

The whole code:

 public Transform cam; //drag firstpersoncamera into inspector.
 bool crouch = false;
 float _lerpPoint = 0;
 Vector3 standPos;
 Vector3 crouchPos;
 
 void Start(){
 crouchPos = new Vector3(cam.transform.position.x, cam.transform.position.y - 1.5f, cam.transform.position.z);// set 1.5 to crouch distance(untested)
  standPos = cam.transform.position;
 }
 
 void Update(){
  if(input.GetKeyDown(KeyCode.LeftControl))
  crouch = !crouch;
 
 if(crouching && _lerpPoint <1){
  _lerpPoint+= 25 * Time.deltaTime;
 //GetComponent<CapsuleCollider>().height = 1;
 }
   else if(!crouching && _lerpPoint > 0){
  _lerpPoint-= 25 * Time.deltaTime;
 //GetComponent<CapsuleCollider>().height = 2;
 }
 
 cam.transform.position.y = Mathf.Lerp( standPos, crouchPos, _lerpPoint);
 }

Im in a bit of a rush, but wanted to answer this for you. The collider change size was just thrown in there, but you can work with it and make it work better...

Let me know if this helps you. If you get this to work, please share your results. If you get the collider part working better, then please share!! :)

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 ClearRoseOfWar · May 05, 2016 at 05:35 PM 0
Share

I didnt test this btw

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

76 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 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 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 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

What is causing the object to fly upwards? 1 Answer

Wondering script Javascript 0 Answers

';' expected. Add semicolon at the end. 2 Answers

Health Will Not Deplete as I shoot Enemy. 0 Answers

Can I use ZeroC Ice with Unity? 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