Free cam mode not working.

Hey all, I decided to make Y a button that would put the main camera in “free cam” mode. However even after Y is pushed I have to be holding my mouse icon at the edge and continually press Y for it to move. Can anyone help me figure out why?

Code:
private var myTransform : Transform;
private var scrollArea = 25;
private var scrollSpeed = 50;
private var Ypressed = false;

function Update () {

if(Input.GetKeyDown("y")){
	FreeCam();
	transform.parent = null;
}

}

function FreeCam () {

Debug.Log("Y was pressed!");

myTransform = Camera.main.transform;
var mPosX = Input.mousePosition.x;
var mPosY = Input.mousePosition.y;
var restoreY = myTransform.position.y;

//Move camera with the arrow keys
transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"), Space.Self);

//Move camera by moving the mouse to the edge of the screen, RTS-Style
if (mPosX < scrollArea) {myTransform.Translate(Vector3.right * -scrollSpeed * Time.deltaTime, Space.Self);}
if (mPosX >= Screen.width-scrollArea) {myTransform.Translate(Vector3.right * scrollSpeed * Time.deltaTime, Space.Self);}
if (mPosY < scrollArea) {myTransform.Translate(Vector3.forward * -scrollSpeed * Time.deltaTime, Space.Self);}
if (mPosY >= Screen.height-scrollArea) {myTransform.Translate(Vector3.forward * scrollSpeed * Time.deltaTime, Space.Self);}

// Restore original camera height
myTransform.position.y = restoreY;

Thanks! Also, if anyone would be so kind, how do toggles works. I know they require ! but I don’t understand anymore than that. D:

1 Answer

1

There is many wayq, one would be to toggle a bool instead of calling FreeCam(), which means something like : useFreeCam = !useFreeCam, and then outside the if(Input…), you call FreeCam if useFreeCam is true.

Your problem is that GetKeyDown is an event happening only once, when the key just got pressed. So FreeCam is called once, and that’s it.