简体   繁体   中英

Unity how to get position from objects that has collided with another object?

Currently im trying to get a T square to lock its position with lines that i have created but im not sure how to get the x position of a line. And right now my T square is able to detect thats it colliding with the lines. Heres my current code.

void LockPostion(float x)
{
    gObjTmp.transform.position = new Vector3 (x, this.transform.position.y, this.transform.position.z);
}

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "Lines") 
    {
        Debug.Log ("Collision with line");
        LockPostion (minlockXPos);
    } 

}

Just like you used this.transform.position to get the position of this Object the script is attached to, col.transform.position should be used to get the position of the other Object returned from the collision function or col.transform.position.x just for the x axis.

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "Lines")
    {
        Vector3 linePos = col.transform.position;
        float linePosX = col.transform.position.x;

        Debug.Log("Collision with line");
        LockPostion(linePosX);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM