Search Unity

Camera rotation "lean"?

Discussion in 'Scripting' started by derrtyones, Dec 17, 2012.

  1. derrtyones

    derrtyones

    Joined:
    Aug 27, 2012
    Posts:
    14
    Hi,

    I am trying to rotate the First Person Controller camera with the Q and E buttons as like you would peek around a corner.
    I tried this script, but I don't see the camera change at all. What am I doing wrong or how can I get this to work? :confused:

    Code (csharp):
    1.  
    2. var leanLeft : boolean = false;
    3. var leanRight : boolean = false;
    4.  
    5. function Update () {
    6.  
    7.     if(Input.GetKeyDown("q")) {
    8.        doLeanLeft();
    9.     }
    10.    
    11.     if(Input.GetKeyDown("e")) {
    12.        doLeanRight();
    13.     }
    14.    
    15.     if(Input.GetKeyUp("q")) {
    16.        doLeanBack();
    17.     }
    18.    
    19.     if(Input.GetKeyUp("e")) {
    20.        doLeanBack();
    21.     }
    22.  
    23. }
    24.  
    25. function doLeanLeft() {
    26.  
    27.     if(leanLeft == false) {
    28.        transform.rotation = Quaternion.Euler(0,0,30);
    29.        leanLeft = false;
    30.     }
    31.  
    32. }
    33.  
    34. function doLeanRight() {
    35.  
    36.     if(leanRight == false) {
    37.        transform.rotation = Quaternion.Euler(0,0,-30);
    38.        leanRight = false;
    39.     }
    40.  
    41. }
    42.  
    43. function doLeanBack() {
    44.     transform.rotation = Quaternion.Euler(0,0,0);
    45. }
    46.  
     
  2. Alastair Callum

    Alastair Callum

    Joined:
    Sep 3, 2012
    Posts:
    71
    Just tested it out, seems to be working fine. Have you attached the script to your camera?

    [EDIT] However, if you are using this in conjunction with the First Person Character Controller script, then you may find that your Lean script is being overridden by the character controller script, as it most likely contains code that sets the Z rotation (or up vector) of the camera to 0 constantly.
     
    Last edited: Dec 17, 2012
  3. derrtyones

    derrtyones

    Joined:
    Aug 27, 2012
    Posts:
    14
    Thanks for your reply. Yes, the script is attached the the Main Camera of the First Person Controller. Any advice on how to get it to work, though?
     
  4. derrtyones

    derrtyones

    Joined:
    Aug 27, 2012
    Posts:
    14
    Ok I see what Callum meant by the FPC overwriting my script. When I disable the Mouselook scripts it works fine, however it seems that each time I use the Q/E buttons it also rotates the camera. Any idea's?

    You can test it by creating a new scene, place a First Person Controller and disable the Mouse Look script attached to the Main Camera of the FPC and attach my script.
     
  5. Alastair Callum

    Alastair Callum

    Joined:
    Sep 3, 2012
    Posts:
    71
    The reason your camera is rotating too is because the script is attached to the camera game object, when you do this, the "transform" component you are rotating is a reference to the transform component of the camera (ie, the object the camera is attached to).

    In order to rotate only your game character (which is what i think you want?) you can either attach it directly to that object, OR create a reference to your character in your lean script by using:

    Code (csharp):
    1. public var myCharacter : Transform;
    You can then assign (drag and drop) your character object onto the script in the inspector. Then in your code you simply need to direct your rotation to affect your "myCharacter" reference:

    Code (csharp):
    1. myCharacter.transform.rotation = Quaternion.Euler(0,0,-30);
    I hope that makes sense. Here's a little tip:

    Basically when you access something like a transform within the code (ie, transform.rotation) you are saying, "Ok, script, whatever you are attached to, go and access the transform component of that object" in your case, that's the camera, so its actually saying Camera.transform.rotation. Whereas if you create a reference to another object and access IT'S transform you will not affect the camera. Compare the two:

    Camera.transform.position
    myCharacter.transform.position

    As you can see, they both have individual tranform components, but it's a case of making sure to access the correct one.

    Now, as for the problem with scripts overriding each other, it may get resolved by making the changes above, if not you may need to look at tweaking the character controller to remove the 'clamp' on Z rotation.

    Good Luck!
     
  6. derrtyones

    derrtyones

    Joined:
    Aug 27, 2012
    Posts:
    14
    Ah thank you so much! You were indeed correct. I now rotate the whole character instead only the camera and it works fine! Now I got a step further!

    The only problem which exists is that sometimes when I turn around and I do a lean, it seems like the camera rotated 180 degrees and does a lean. I guess that's because of the scripts getting overwritten? So I have to look at the clamp of the FPC?