0

I'm currently creation a game in unity were I've made it so the background and the obstacles in the game constantly "respawns" and creates a loop. Every thing was working fine until I added a second script to the obstacles to make them move vertically. After adding the script I received the error: "Cannot cast from source type to destination type".

This the script to make the objects loop (Cs):

public class BgLooper : MonoBehaviour {

    int numBGPanels = 10;

    float cloudMax = 2.2f;
    float cloudMin = -0.2f;

    void Start() {
        GameObject[] clouds = GameObject.FindGameObjectsWithTag ("Cloud");

        foreach (GameObject cloud in clouds) {
            Vector3 pos = cloud.transform.position;
            pos.y = Random.Range(cloudMin, cloudMax);
            cloud.transform.position = pos;
        }
    }

    void OnTriggerEnter2D(Collider2D collider) {
        Debug.Log ("Triggered: " + collider.name);

        float widthOfBGobject = ((BoxCollider2D)collider).size.x;

        Vector3 pos = collider.transform.position;

        pos.x += widthOfBGobject * numBGPanels;

        if (collider.tag == "Cloud") {
            pos.y = Random.Range(cloudMin, cloudMax);
        }

        collider.transform.position = pos;
    }
}

And this is the script to make the obsticales move (Js):

var maxX = 6.1;
var minX = -6.1;
var maxY = 4.2;
var minY = -4.2;

var moveSpeed = 0.3;

private var tChange: float = 0; // force new direction in the first Update
private var randomX: float;
private var randomY: float;

function Update () {
    // change to random direction at random intervals
    if (Time.time >= tChange){
        randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
        randomY = Random.Range(-2.0,2.0); //  between -2.0 and 2.0 is returned
        // set a random interval between 0.5 and 1.5
        tChange = Time.time + Random.Range(0.5,1.5);
    }
    transform.Translate(Vector3(randomX,randomY,0) * (moveSpeed / 15) * Time.deltaTime);
    // if object reached any border, revert the appropriate direction
    if (transform.position.x >= maxX || transform.position.x <= minX) {
        randomX = -randomX;
    }
    if (transform.position.y >= maxY || transform.position.y <= minY) {
        randomY = -randomY;
    }
    // make sure the position is inside the borders
    transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
    transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);
}
4

1 回答 1

0

您应该能够访问Collider2D上的bounds属性,而不是像这样使用强制转换。


void OnTriggerEnter2D(Collider2D collider) {
    Debug.Log ("Triggered: " + collider.name);

    //change this line
    float widthOfBGobject =  collider.bounds.size.x;

    Vector3 pos = collider.transform.position;

    pos.x += widthOfBGobject * numBGPanels;

    if (collider.tag == "Cloud") {
        pos.y = Random.Range(cloudMin, cloudMax);
    }

    collider.transform.position = pos;
}

这应该是您OnTriggerEnter2D在更改后的样子。

于 2014-09-02T19:11:04.007 回答