手机重力感应代码
//Ð?ÇòµÄÌùÍ?
var round : Texture2D;
//Ð?ÇòÔÚÆÁÄ?ÖÐÏÔÊ?µÄX Y×ø?ê
var x = 0;
var y = 0;
//Ð?ÇòÆÁÄ?ÏÔÊ?µÄ×î?ó X Y ??Î?
var cross_x = 0;
var cross_y = 0;
function Start(){
//?õÊ?????Öµ
cross_x = Screen.width - round.width;
cross_y = Screen.height - round.height; }
function OnGUI () {
//ÕûÌåÏÔÊ? x y z ÖØÁ??ÐÓ?µÄÖØÁ??ÖÁ?
GUI.Label(Rect(0,0,480,100),"position is " + Input.acceleration);
//?æÖÆÐ?Çò
GUI.DrawTexture(Rect(x,y,256,256),round); }
function Update(){
//?ù?ÝÖØÁ??ÖÁ?ÐÞ?ÄÐ?ÇòµÄÎ?ÖÃÕâÀï?ËÒÔ30µÄÒâË?ÊÇÈÃÐ?ÇòÒÆ??µÄ?ìÒ?Ð?
x += Input.acceleration.x * 30;
y += -Input.acceleration.y * 30;
//?ÜÃâÐ?Çò???öÆÁÄ?
if(x < 0){
x = 0;
}else if(x > cross_x){
x = cross_x;
}
if(y < 0){
y = 0;
}else if(y > cross_y){
y = cross_y;
}
}
//----------------------------------------------------------------
----------------------------------
?ýInput.accelerationÈ?Äõ?µ?Ç?ÊÖ?úµÄÖØÁ??ÐÓ??ÖÁ? //----------------------------------------------------------------
----------------------------------
????Ò??ºspeed
public var simulateAccelerometer:boolean = false; var speed = 10.0;
function Update () {
var dir : Vector3 = Vector3.zero;
if (simulateAccelerometer)
{
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
}
else
{
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per
frame...
}
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
}
Ò??ÉÒÔ?ÑËÙ?È???ÉÁ?
?????þ?ºForce
public var force:float = 1.0;
public var simulateAccelerometer:boolean = false; function FixedUpdate () {
var dir : Vector3 = Vector3.zero;
if (simulateAccelerometer)
{
// using joystick input instead of iPhone accelerometer
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
}
else
{
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = Input.acceleration.y;
dir.y = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
}
rigidbody.AddForce(dir * force);
}
//----------------------------------------------------------------
----------------------------------
public var simulateAccelerometer:boolean = false; var speed = 10.0;
function Update () {
var dir : Vector3 = Vector3.zero;
if (simulateAccelerometer)
{
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
}
else
{
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per
frame...
}
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed); }
??ÖÆ?úÂë
Ò??ÉÒÔ?ÑËÙ?È???ÉÁ?
?????þ?ºForce
view sourceprint?
public var force:float = 1.0;
public var simulateAccelerometer:boolean = false; function FixedUpdate () {
var dir : Vector3 = Vector3.zero;
if (simulateAccelerometer)
{
// using joystick input instead of iPhone accelerometer
dir.x = Input.GetAxis("Horizontal");
dir.y = Input.GetAxis("Vertical");
}
else
{
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = Input.acceleration.y;
dir.y = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
}
rigidbody.AddForce(dir * force);
}
??ÖÆ?úÂë
?öÈË?Ð?õ????Ò??Ù?ØÆðÀ??È?ÏÁé?î????Ó?ÁéÃô???????þ?Ù?ØÆðÀ??ßÓÐ?ßÐÔ???º?åÃ?ÏÔ??
//--------------------------------------------------------------------------------------------------
virtual