- Home /
How can I disable controls?
I know this question has been asked, but I can't get it to fire right. I've read a few solutions, but can't seem to work it out. I'm trying to cut controls for cinematic scenes with the first person controller.
Right now I have a wonky set up I think..
I have a script on both the camera and the player controller.
Camera Script
#pragma strict
function EnableControls(){
var CamControls = GetComponent.<MouseLook>();
CamControls.enabled = CamControls.enabled;
var PlayerControls: GameObject;
PlayerControls.GetComponent(DisablePlayerControls).EnablePlayerControls();
}
function Start () {
var CamControls = GetComponent.<MouseLook>();
CamControls.enabled = !CamControls.enabled;
}
function Update () {
}
The script on the Player controll
#pragma strict
function EnablePlayerControls(){
var MouseLook = GetComponent.<MouseLook>();
var Motor = GetComponent.<CharacterMotor>();
var Controller = GetComponent.<FPSInputController>();
MouseLook.enabled = MouseLook.enabled;
Motor.enabled = Motor.enabled;
Controller.enabled = Controller.enabled;
}
function Start () {
var MouseLook = GetComponent.<MouseLook>();
var Motor = GetComponent.<CharacterMotor>();
var Controller = GetComponent.<FPSInputController>();
MouseLook.enabled = !MouseLook.enabled;
Motor.enabled = !Motor.enabled;
Controller.enabled = ! Controller.enabled;
}
function Update () {
}
I created an animation on the main camera using the built in animation feature. I try to fire an event from the animation which is a function from an instance of the script on the player control.
Errors I get are..
NullReferenceException: Object reference not set to an instance of an object DisableCameraControls.EnableControls () (at Assets/Scripts/MenuScripts/DisableCameraControls.js:12)
AnimationEvent 'EnableCameraControls' has no receiver!
Thanks in advanced to anyone who can help me sort this out. Thanks guy :)
The lazy way to do this temporarily is to add a variable to see whether the animation is playing and if don't let the movement code run. It's not a permanent as deleting and re adding the game object, but could work while you are waiting for a solution
Answer by aldonaletto · May 18, 2013 at 05:21 PM
In order to disable controls in the First Person Controller prefab, a better alternative is to get all scripts of interest at Start and just enable/disable them when needed - like this:
#pragma strict
private var MLook1: MouseLook;
private var MLook2: MouseLook;
private var ChMotor: CharacterMotor;
function Start(){
MLook1 = GetComponent(MouseLook);
MLook2 = Camera.main.GetComponent(MouseLook);
ChMotor = GetComponent(CharacterMotor);
}
function EnablePlayerControls(enable: boolean){
MLook1.enabled = enable;
MLook2.enabled = enable;
ChMotor.enabled = enable;
}
Notice that you don't need a camera specific script, since this one does the job - just call EnablePlayerControls(true) to enable the controls, or pass false to disable them. You don't need either to disable FPSInputController - this script just passes info to CharacterMotor. Actually, you may have two different behaviours: disabling FPSInputController disables control but the character finishes its current movement - if it's falling, for instance, it will stop only when reaching ground. Disabling CharacterMotor, on the other hand, will stop the character immediately - it may get frozen in the middle of a fall, in the example above.
NOTE:Don't use variables with the same name of scripts or other components - like MouseLook, for instance. The variable hides the original class (script or component) and you get lots of errors or warnings.
Yes, and most programmers forget the damned variable and almost get crazy thinking that Unity is defective. To make things worse, usually the hidden component is a very popular one, like Rigidbody: Unity suddenly starts reporting that AddForce isn't part of Rigidbody, and that sort of things.
hmm... I'm not sure this will work in my instance... The thing is I need to enable and disable controls on command. I need to be able to do it for every cut scene. I need a script on the camera to control this because the fire event feature for animation only works with script attached the the camera...
I noticed a major problem in my code before, so I've been fixing it up a little...
Here's the Player script code:
#pragma strict
// This function will Disable Player Controls
function DisablePlayerControls(){
var $$anonymous$$ouseLook = GetComponent.<$$anonymous$$ouseLook>(); // variable for mouse look of player
var $$anonymous$$otor = GetComponent.<Character$$anonymous$$otor>(); // variable for character motor of player
var Controller = GetComponent.<FPSInputController>(); // variable for controller of player
$$anonymous$$ouseLook.enabled = !$$anonymous$$ouseLook.enabled; // sets mouse look of player to not enabled
$$anonymous$$otor.enabled = !$$anonymous$$otor.enabled; // sets motor of player to not enabled
Controller.enabled = ! Controller.enabled; // sets controller of player to not enabled
}
// This function will enable Player controlls.
function EnablePlayerControls(){
var $$anonymous$$ouseLook = GetComponent.<$$anonymous$$ouseLook>(); // variable for mouse look
var $$anonymous$$otor = GetComponent.<Character$$anonymous$$otor>(); // variable for character motor
var Controller = GetComponent.<FPSInputController>(); // variable for controller
$$anonymous$$ouseLook.enabled = $$anonymous$$ouseLook.enabled; // set mouse look of player to enabled
$$anonymous$$otor.enabled = $$anonymous$$otor.enabled; // set motor of player to enabled
Controller.enabled = Controller.enabled; // set controller of player to enabled
}
function Start () {
}
function Update () {
}
Here's the camera script code
#pragma strict
// This function will disable all controls (Add call to Disable player controls from script Disable Player Controls
function DisableAllControls(){
var CamControls = GetComponent.(); // creats variable for mouse look of camera
CamControls.enabled = !CamControls.enabled; // Disables the mouse look.
}
// This function will enable all controls cam/player add call to enable controls from disableplayer controls.
function EnableAllControls(){
var CamControls = GetComponent.(); // creates a variable to hold $$anonymous$$ouselook component of camera
CamControls.enabled = CamControls.enabled; // Sets $$anonymous$$ouseLook to enabled.
}
function Start () {
}
function Update () {
}
Notice that I have two functions in the camera script code that I'll call from the animations to disable and enable controls. The problem is I don't know how to access functions from my player script to get them to fire in the camera script...
Sorry if there's a better way... let me know. I'm not the most logical guy runnin, lol.
wait, wait. I never thought of functions with arguments. Your method will work better with one script. I just need a script on my camera with one function called controls that takes a boolean like you said...
I just need to know how to access the components of another game object properly...
can you help with that?
So if I have one script on the camera like so
#pragma strict
// This function will handle enable and disable of all controls.
function AllControls(var Enabled : boolean){
if(!Enabled){
var CamControls = GetComponent.<$$anonymous$$ouseLook>(); // creats variable for mouse look of camera
CamControls.enabled = !CamControls.enabled; // Disables the mouse look.
}else{
var CamControls = GetComponent.<$$anonymous$$ouseLook>(); // creats variable for mouse look of camera
CamControls.enabled = CamControls.enabled; // Disables the mouse look.
}
function Start () {
}
How can I access the player controls on the FPC in this script and add them to enable and disable. I have a bit of a problem with that. I'm not really sure how to get into other objects scripts and components very well...
function Update () {
}
Your answer

Follow this Question
Related Questions
How to get a rigidbody to move left and right? 1 Answer
Disable/Enable Colliders 1 Answer
Recommended way to delay controls momentarily? 2 Answers
How to disable collision2D damage when I am not attacking? 0 Answers
Player or character select 2 Answers