- Home /
Mouse Look Script Pause
Hey guys, I'm using the "mouse look" script. I'll paste it here, its only tiny. I'm sure most fo you have seen it. So what I wanted to do with it is, When I press, Either 1 or 2, for the script to pause? or perhaps deactivate. And then most likely activate again when pressed again. Basically I want a way to stop is on keypress :) Any ideas??
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
Answer by Ouija · Nov 30, 2012 at 09:59 PM
This is what I got, I tired to use comment, but the code gets messy?
private bool isEnabled = true;
void Update()
{
//KeyReleased
if (!Input.GetKeyUp(KeyCode.A))
{
//Toggle the enabled flag
isEnabled = !isEnabled;
}
if (!isEnabled)
return;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
Camera starts fine but Nothing seems to happen when I press the A key. The camera still moves around freely. I must be missing something LOL
That's my fault. I had a typeo in my answer edit above. Change the " if (!Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A)) " - get rid of the ! so that it reads:
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A))
Basically right now you're checking if the A key is NOT released, which was my typeo. Sorry for the confusion.
Very Cool! it works like a charm thanks alot :) We can edit this post so it has the right answer if you wish
Hey kaolas last night I stumbled upon something I never thought about lol I have it set up so when you press either button liek we discussed, but there is a problem One button makes a menu come up, and the second button makes a second menu come up
so you end up getting stuck in a loop when toggling the button, ex. I press 1 and the menu comes up, it can close fine if you press 1
but when you press 1, and then 2 it starts to get messy I can try to explain better if needed!
Added a response at the bottom of my answer above. Let me know if it doesn't make sense.
Answer by ShadowAngel · Nov 30, 2012 at 06:06 PM
evry component has .active parameter. so just use something like:
gameObject.GetComponent(MouseLook).active = false;
Answer by kaolas · Nov 30, 2012 at 06:13 PM
If I understand your question correctly, you want to "shortcut" the Update() function when a key is pressed.
To do that, add this at the beginning of your Update():
if (Input.GetKey(KeyCode.Keypad1) || Input.GetKey(KeyCode.Keypad2))
return;
This will cause the Update() to stop processing so long as either the 1 or 2 keys are held down. If you want to do a toggle, rather than a hold, you would need to look into the Input.GetKeyDown() and Input.GetKeyUp() functions and based on those, define when you toggle the update on or off.
EDIT (response to comment):
The Input.GetKey() will only tell you if a key is currently being pressed. If you want it to be on, then you press a key and it goes to off, then press a key and it goes back to on, that's the toggle situation I described.
Here are the "events" you can hook into:
void Update()
{
//KeyPressed
if (Input.GetKeyDown(KeyCode.Keypad1))
{
//The user has just now pressed the key down.
}
//KeyHeldDown
if (Input.GetKey(KeyCode.Keypad1))
{
//The user is currently holding the key down.
}
//KeyReleased
if (Input.GetKeyUp(KeyCode.Keypad1))
{
//The user has just now released the key.
}
}
What you will most likely want to do is hook into the "KeyReleased" functionality and toggle the value of whether you should process the update or not:
private bool isEnabled = true;
void Update()
{
//KeyReleased
if (Input.GetKeyUp(KeyCode.Keypad1))
{
//Toggle the enabled flag
isEnabled = !isEnabled;
}
if (!isEnabled)
return;
... the rest of your update code ...
}
This will default the update to be on because isEnabled is true when it is initialized, then let you press a key to toggle it on or off (it just negates its prior value).
The more you get into Input and want finer control over what you're doing with it, the more I would recommend existing plugins like FingerGestures. There's no reason to reinvent the wheel and this and other plugins like it do a very good job of encapsulating the input events.
EDIT: Fixed typeos in the GetKeyUp() checks. EDIT: Response to comment:
If you want two buttons to toggle two pieces of functionality, you should have two different booleans to track the state of each of those pieces of functionality.
//Functionality1
if (!Input.GetKeyUp(KeyCode.A))
{
//Toggle the enabled flag
isFunction1Enabled = !isFunction1Enabled;
}
//Functionality2
if (!Input.GetKeyUp(KeyCode.B))
{
//Toggle the enabled flag
isFunction2Enabled = !isFunction2Enabled;
}
if (isFunction1Enabled)
{
//Do functionality 1
}
if (isFunction2Enabled)
{
//Do functionality 2
}
This will let you independently toggle each functionality without respect for the toggle state of the other functionality. You're no longer shortcutting the Update(), but rather controlling when things happen or don't happen. Of course, if both are false, your Update() won't do anything.
Thanks for the answers! $$anonymous$$aolas I believe you are on the right track, only thing is Id like the "look" script to be on as default , just given the option to turn it on and off afterwards with the 1 or 2 key Sorry for leaving out that bit of info!
hmm. I think I wrote it out correct. but the camera still seems to move freely