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?
Its working but only at a small scale. try this Code (csharp): function Update () { if (Input.GetButtonDown ("Look Up")) { transform.Rotate(Vector3.up *100* Time.deltaTime); } } I put a space in between Look and Up only 4 my convinience AC
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...
Code (csharp): var upAngle = 45.0; function Update () { transform.Rotate(Input.GetButton("LookUp") * upAngle, 0, 0); } or Code (csharp): var upAngle = 45.0; function Update () { transform.Rotate(Vector3.up * Input.GetButton("LookUp") * upAngle); } 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.
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): function Update() { transform.Rotate(Vector3(0, Input.GetAxis("Vertical"), 0) * 100 * Time.deltaTime); } 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.
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...
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.
For future reference, there's a quirky syntax you can use as an "inline if" statement: Code (csharp): value = (GetButton("Jump") ? 1.0 : 0.0); If GetButton returns true, value will be set to 1.0; if not, 0.0.
cool tidbit! negative and positive keys don't work though correct? it's still just true or false right? anyway... getting ot...
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...
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!
Soeone should post this on the Unify Wiki Script Archive... hint hint :wink: I've even added an entry ready to fill out.