- Home /
Using a Key to Activate Camera Mouse Look
So I'm creating this third person game using the Third Person Controller script from Unity's 3D platform tutorial to move my character. I want to add on another feature which allows the player to hold down a specific key (for example the F-key) that will freeze the Third Person Controller (not allowing the player to move) and activate my mouse look script. What script would I need to do this?
Answer by MC HALO · Dec 30, 2010 at 07:59 PM
function Update (){
if (Input.GetKey (KeyCode.F)){
GetComponent(" Put the script name over here ").enabled = false;
}
}
hope it helps :) this will disable the script when the F key is pressed. the script above only disables the script. The script Below will allow you to on and off the script with the same key:
var OnScript : boolean = true;
function Update() {
if(Input.GetKey(KeyCode.F) && OnScript ){
GetComponent(AnimationScript).enabled = false; OnScript = false;
} else if (Input.GetKey(KeyCode.F) && OnScript == false ){
GetComponent(AnimationScript).enabled = true;
OnScript = true;
} }
it may look confusing but it is easy to under stand on the new script on top of the update function i have created a check box using a boolean. The check box will be set as true when you run the game and the player script will be enabled. if you press the F key once it will disable the script. You may notice underneath the line
**" if(Input.GetKey(KeyCode.I) && OnScript ){"**
it says OnScript = false this is disabling the check box if it is off the script goes off. and if you press the F key again it will make the check box true again and the script will come back on. I hope this helps :)
if you have any questions regarding this script please ask :)
Try this :
var OnScript : boolean = true;
function Update() {
if(Input.GetKeyUp(KeyCode.K) && OnScript) {
OnScript = false;
GetComponent(AnimationScript).enabled = false;
}
else if (Input.GetKeyUp(KeyCode.K) && !OnScript) {
OnScript = true;
GetComponent(AnimationScript).enabled = true;
}
}
the problem was using Input.GetKey()returns true while the user is holding the key. if you use Input.GetKeyUp() this would return true only when the user releases the key
So that works. Now I need to figure out how to reactivate it when the F-$$anonymous$$ey is pressed again.
what you want to shut it off ? if so you will need to do the following :
Oh my mistake. I wanted to activate it then shut it off. I'm having a little problem though. It works to shut it off and on, although sometimes I have to hold it down to switch it. Is that just how the script works or is there something that isn't working right?
no you should be able to tap it. but you have to wait 2 sec before you can hit it again if this is a problem let me know and i will come up with a idea again :)
Your answer
Follow this Question
Related Questions
how to create a TPS camera view? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gun clipping through walls in third person shooter 1 Answer
How to get an object's direction relative to the camera? 3 Answers
Using the "ThirdPersonController" Script without its Camera? 0 Answers