Search Unity

MouseLook through keys

Discussion in 'Scripting' started by yangmeng, Jan 4, 2007.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    I am using the "MouseLook" script, but I would also like to be able to rotate the camera through keyboard input. I added a "LookUp" Element to the Input Manager, made the following script, added it to the camera and got no results.

    function Update () {
    if (Input.GetButtonDown ("LookUp")) {
    transform.Rotate(Vector3.up * Time.deltaTime);
    }
    }

    It seems it should work but I must be missing something. Any ideas?
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Its working but only at a small scale. try this
    Code (csharp):
    1. function Update () {
    2. if (Input.GetButtonDown ("Look Up")) {
    3. transform.Rotate(Vector3.up *100* Time.deltaTime);
    4. }
    5. }
    I put a space in between Look and Up only 4 my convinience
    AC
     
  3. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Your solution makes sense to me, but when I tried it, instead of the camera rotating to "look up" it just jerks to the side and returns to it's prior position... Not what I expected. Weird. I must have something else there that's off...
     
  4. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    Code (csharp):
    1.  
    2. var upAngle = 45.0;
    3.  
    4. function Update () {
    5. transform.Rotate(Input.GetButton("LookUp") * upAngle, 0, 0);
    6.  
    7. }
    or
    Code (csharp):
    1.  
    2. var upAngle = 45.0;
    3.  
    4. function Update () {
    5. transform.Rotate(Vector3.up * Input.GetButton("LookUp") * upAngle);
    6.  
    7. }
    maybe? GetButtonDown only is true the frame it gets pushed not the whole time it's down. GetButton returns true while it's down. don't know why it jerked to the right rather than up.

    edit: this will return to zero when the key is released.

    edit2: starmanta and i posted at the same time. i would agree i'd look at GetAxis but you may or may not want that. you should be able to make it work either way. also, i'm not sure if it's valid to multiply Vector3.up the way I did in my second function. you'd have to try.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'm not sure why your object is behaving that way now, yangmeng; it seems like Targos's code should work fine.

    However, there is a preferred way to do what you're doing.
    You're probably going to want the ability to move both up and down; with your code you'll need two separate if statements, as opposed to:
    Code (csharp):
    1.  
    2. function Update() {
    3. transform.Rotate(Vector3(0, Input.GetAxis("Vertical"), 0) * 100 * Time.deltaTime);
    4. }
    5.  
    Furthermore, that method makes it simpler to modify for adding the horizontal axis.

    Try that method; it may fix your problem and simplify your code in one swoop.
     
  6. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Thanks guys. StarManta, your script worked well but only if I turned off the MouseLook script. With that script on I just get that move a little and then swing back motion. I want to keep the mouse as the default option but also allow users to move the camera by keys. Should the two scripts work together?
    pete, I tried your two scripts and played around with them some but Unity kept giving me exceptions.
    I think the issue is some kind of conflict between the generic MouseLook script and the script I am trying to add...
     
  7. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    oops. GetButton is a boolean. you can't multiply it by a float. you'd have to use an if statement but that's probably not what you want. GetAxis is a value from -1 to 1 (it think). so it can be multiplied by a float and would work better.

    but i took a quick look and it won't work with the mouselook script also attached. it looks like mouselook resets the rotation to stay where the mouse is and probably overrides what you're trying to do. i don't really know c# though.

    one idea is make your camera a child of a game empty. put mouselook on the empty and your keylook script on the camera (or the other way around). might get some weird results though. if i get motivated i'll try to play with it.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    For future reference, there's a quirky syntax you can use as an "inline if" statement:
    Code (csharp):
    1.  
    2. value = (GetButton("Jump") ? 1.0 : 0.0);
    3.  
    If GetButton returns true, value will be set to 1.0; if not, 0.0.
     
  9. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    cool tidbit! negative and positive keys don't work though correct? it's still just true or false right? anyway... getting ot...
     
  10. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Thanks, pete. That was a good idea and it worked great.
     
  11. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    glad you got it working! :D
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    With the tidbit I just posted? No. But GetAxis does much the same thing for a negative and positive.
     
  13. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    here you go yangmeng. note it's got everything turned on. if you're doing a standard MouseX on the character and MouseY on a camera control, you'll want to turn off the KeyLook stuff in the if MouseX statement. Otherwise your character will rotate. pretty much you need to modify it for your needs. the script's overly commented (sorry). hopefully it makes sense. i didn't figure out how to stop jitter yet - when keyUp and mouseDown happen they fight each other. i'll look at it again someday...
     

    Attached Files:

  14. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Great. I'll give it a look and play with it. I am sure the comments will be of benefit and I imagine I'll be able to learn a lot about scripting from a new example, so thanks!
     
  15. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    welcome. it was good learning for me too. let me know if you can't figure out how to modify it.
     
  16. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Soeone should post this on the Unify Wiki Script Archive... hint hint :wink:

    I've even added an entry ready to fill out.
     
  17. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    done. i couldn't figure out how to last night! think i get it now...