- Home /
How to freeze a character controller camera
Ok so i have this awesome keypad script finally finished thanks to all the help i have received on here, and now i've been asked to update it to handle some camera controls. What i need is to be able to turn off me MouseLook script and center the Main Camera on my First Person Character Controller. this way the camera is facing the directly at the door, and it wont move while the player interacts with the keypad gui. ive looked at lots of different examples but i haven't been able to get anything to work. any help would be much appreciated, and if possible i would prefer all answers in C#. im afraid i dont have any starting code to work with.
Just disable the FPC camera and enable the secondary camera.
(C# is not my native language) :
public Camera fpsCam;
public Camera kbdCam;
void UseFpsCam()
{
kbdCam.enabled = false;
fpsCam.enabled = true;
}
void Use$$anonymous$$boardCam()
{
fpsCam.enabled = false;
kbdCam.enabled = true;
}
thanks for the quick reply, however after testing this code it does not work. while the camera appears to be disabled it still moves and projects. Also it forces me to add a second camera to my player controller which i have just been informed is not allowed. i need this scrip simply make it so that the player camera will no longer follow the mouse while the player is within a trigger i have set up.
Answer by LunaArgenteus · Apr 21, 2013 at 06:25 PM
For your camera movement, you'll want a script that is able to manipulate its position and rotation. If you simply need the camera to behind and looking at the player, assuming you're storing a reference to the player's gameObject you can set up something like this:
//you should probably declare these floats as constant fields
float prefferredCamDist = 5f;
float arbitraryLerpModifier = 0.5f;
Vector3 desiredPosition = playerObject.transform.position - playerObject.transform.forward*prefferredCamDist;
//In this case, the desired position is DIRECTLY behind the player - you may wish to add a y component so the desired
//position is also slightly above the player
//From here, you can either make the camera jump to the desired position, or you can use Lerp / SmoothDamp for smooth motion
//In this example, I'll use lerp
this.transform.position = Vector3.Lerp(this.transform.position, desiredPosition, arbitraryLerpModifier);
//increase the value of arbitraryLerpModifier modifier to increase the speed at which your camera approaches the desired position
//Multiply by Time.deltaTime if you want your camera's motion to be time dependant
Quaternion desiredRotation = Quaternion.LookRotation(playerObject.transform.position - this.transform.position);
//alternatively, if you want to look directly at the keypad, change playerObject to your keyPadObject
//From here, again, you can have the camera look directly at your target, or you can use smoothing
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, desiredRotation, arbitraryLerpModifier);
Just make sure that you only reach this code when it's relevant, i.e. if your KeyPadEnabled variable is true. Otherwise, use your normal camera code.
actually I'm using a first person player controller from the unity assets. all i really need it to do is stop following the mouse while my player is inside a trigger. something as simple as disabling the mouselook script would suffice. this is a school project and i have a lot of odd restrictions. i can only use a 1st person controller, i cant add anything to said controller unless there is no other possible answer. and i can only work with the assets for said controller as they are. meaning that the keypad script i have been working on in other questions is the only script im allowed to use for this, if that is at all possible. can you think of any ways that this can work or will i need to inform my professor that we will need to edit the 1st person controller?
Oh, well if all you need to do is disable the components, you should be able to store a reference to the two $$anonymous$$ouseLook components (one on the controller, one on the camera) and just use the "enabled" property to activate and deactivate them.
ive tried that. i created a gameobject variable named player to represent the controller. and then tried the GetComponent script to set the enabled property of both of those scripts but the script isnt compiling. it looks kinda like this.
player.GetComponent<script>($$anonymous$$ouseLook);
player.$$anonymous$$ouseLook.enabled = false;
im still trying to figure out how to reference the scripts. honestly I would never have gotten this far without the help of this community.
It really would be easier (and more efficient!) to declare your reference variables as $$anonymous$$ouseLook objects ins$$anonymous$$d of using GameObjects and getting the component off of them whenever you need to use them (Also, remember that there are TWO $$anonymous$$ouseLook scripts in the First Person Controller prefab that you need to keep track of).
However, for the sake of learning, if you WERE to store references to GameObjects and access the $$anonymous$$ouseLook components when you needed them, the syntax would look more like this:
$$anonymous$$ouseLook mouseLookComponent = player.GetComponent<$$anonymous$$ouseLook>();
mouseLookComponent.enabled = false;
thank you thank you thank you. i didnt realize it until you said it on here that i could set the script name as a target. just tried the code to target the $$anonymous$$ouseLook Script and it worked like a dream. ive also taken the code you posted for me and saved it for future reference just in case. thank you so much for all of the help. your a real life saver Luna.
Your answer
Follow this Question
Related Questions
I want to create an interactive right analog MouseLook? 0 Answers
Multiple Cars not working 1 Answer
Third-person camera snaps to 270 degrees on activation 1 Answer
Disabling a child script within the parent. 0 Answers
Distribute terrain in zones 3 Answers