Wednesday, March 06, 2013

Unity Engine: mouse and key input

Sample JS:
function Update () {
this.gameObject.transform.position.x = Input.mousePosition.x;
this.gameObject.transform.position.y = Input.mousePosition.y;
// only capture once. will not be true until key is released
if(Input.GetKeyDown(KeyCode.R)){
print("rotate");
this.gameObject.transform.Rotate(0, 0, 6);
}
// will not be true until button is released
if(Input.GetMouseButtonDown(0)){
        print("left mouse button down");
this.gameObject.transform.Rotate(0, 0, -6);
}
// auto fire mode. will return true as long as key is down
    if(Input.GetKey(KeyCode.R) && Input.GetKey(KeyCode.LeftShift)){

this.gameObject.transform.Rotate(0, 0, 10*Time.deltaTime);
}
}


Used a new coordinate system. origin of plane at bottom left. to match with mouse screen coordinate
switched the camera to its original orientation. no rotation. orthographic. x, y, z = (w/2, h/2, 0)

Sample of OBJ file:
# This file uses centimeters as units for non-parametric coordinates.

v 0.000000 1.000000 0.000000
v -1.000000 1.000000 0.000000
v 0.000000 0.000000 -0.000000
v -1.000000 0.000000 -0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
f 3/1/4 4/2/3 2/4/2 1/3/1


No comments: