Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RayneAvalon · Apr 19, 2013 at 08:03 PM · c#cameramouselook

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.

Comment
Add comment · Show 2
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 AlucardJay · Apr 19, 2013 at 08:07 PM 0
Share

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;
 }
avatar image RayneAvalon · Apr 21, 2013 at 05:54 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · 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 RayneAvalon · Apr 21, 2013 at 06:31 PM 0
Share

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?

avatar image LunaArgenteus · Apr 21, 2013 at 06:50 PM 0
Share

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.

avatar image RayneAvalon · Apr 21, 2013 at 07:14 PM 0
Share

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.

avatar image LunaArgenteus · Apr 21, 2013 at 07:30 PM 0
Share

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;
avatar image RayneAvalon · Apr 21, 2013 at 07:43 PM 0
Share

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.

Show more comments

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

13 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

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


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