- Home /
Why my c# script doesn't work?
Now I have been trying to edit the "MouseLook" script on my own. Now I want to disable "mouselook" when i press "i". Question is why it works sometimes and other times dont? I mean sometimes it looks away when i move the mouse even i am pretty sure that I pressed "i". using UnityEngine; using System.Collections;
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;
public bool Inventoryenabled = false;
float rotationY = 0F;
void Update ()
{
if(Input.GetKey("i")){
Inventoryenabled = !Inventoryenabled;
}
if(!Inventoryenabled){
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 (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
}
Answer by Jessespike · Jan 28, 2016 at 08:32 PM
From the manual:
Input.GetKey
Returns true while the user holds down the key identified by name. Think auto fire.
GetKeyDown
Returns true during the frame the user starts pressing down the key identified by name.
GetKeyUp
Returns true during the frame the user releases the key identified by name.
Try Input.GetKeyDown instead.
It didnt work but i solved my problem. I remembered that I set Time.timeScale to 0 in another script so, I multiplied all variables by Time.deltatime. Still thanks for the help though.
Your answer
