Google Cardboard Magnet Refresh在Unity3d中不起作用

时间:2014-12-17 05:19:53

标签: unity3d

我正在使用谷歌纸板制作一个Unity项目,我的设计基于Cardboard Catapult,纸板磁铁跳球,它第一次运行,但每当游戏重新启动时,使用Application.LoadLevel() ,球不再跳;它只是毛刺。我used this script。我已经使用测试应用程序测试了这个脚本(磁体传感器脚本),无论何时拉动磁体,文本都会改变颜色并且它始终有效。这是我的球控制脚本(我称之为磁铁传感的脚本):

#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
var Hit01 : AudioClip;
var Hit02 : AudioClip;
var Hit03 : AudioClip;
var distToGround : float;

function Start () {
    // Getting the distance from the center to the ground.
    distToGround = collider.bounds.extents.y;
}

function Update ()
{
    //Handle ball rotation.
    var rotation : float = Input.GetAxis(“Horizontal”) * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotation);
    MagnetSensor.OnMagnetPull += JumpOnMagnet;    //important
}

function JumpOnMagnet () {   //important
    rigidbody.velocity.y = jumpHeight;    //important
}    //important

function IsGrounded () : boolean { //Check if we are on the ground. Return true if we are else return null.
    return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}

function OnCollisionEnter () {
    var theHit = Random.Range(0, 3);
    if (theHit == 0)
    {
        audio.clip = Hit01;
    }
    else if (theHit == 1)
    {
        audio.clip = Hit02;
    }
    else {
        audio.clip = Hit03;
    }
    audio.pitch = Random.Range (0.9,1.1);
    audio.Play();
}

这是重启的脚本:

#pragma strict
var maxFallDistance = -10;
private var isRestarting = false;
var level : String;
var GameOverSound : AudioClip;

function Update ()
{
    if (transform.position.y <= maxFallDistance)
    {
        if (isRestarting == false)
        {
            RestartLevel();
        }
    }
}

function RestartLevel () {
    isRestarting = true;
    audio.pitch = 1;
    audio.clip = GameOverSound;
    audio.Play();
    yield WaitForSeconds (audio.clip.length);
    Application.LoadLevel(level);
}

请让我知道可能存在的问题以及如何解决这些问题。此外,如果您能指出我可以提供帮助的任何网站或资源,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

不要在Update()函数中执行MagnetSensor.OnMagnetPull += JumpOnMagnet;。在Start()中调用它,因此只添加一次。

此外,您应该添加一个OnDestroy()函数并在其中调用MagnetSensor.OnMagnetPull -= JumpOnMagnet;