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)
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!! :)
Your answer
Follow this Question
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