Kia Jun
Kia Jun

Reputation: 87

Unity how to lock only a certain axis using touchscript?

does anyone know how to lock only a certain axis while another axis is not locked? Currently im using touchscript from the unity assets store and with my current code, i could lock the x axis/position of a gameobject but when i tried to lock the y axis after collide with another object, it locks both the X and Y axis. Heres my current code:

public GameObject gObjTmp;
float LinePosX;
float LinePosY;

bool lockPosX = false;
bool lockPosY = false;

public float minYHeight;
public float maxYHeight;


public void LockPositionX()
{
        //Debug.Log ("Lock positionX");
        gObjTmp.transform.position = new Vector3 ((LinePosX + 4), this.transform.position.y, this.transform.position.z);
        //Debug.Log (gObjTmp.transform.position);
    lockPosX = true;
    lockPosY = false;
    Debug.Log (lockPosX);
    Debug.Log (lockPosY);
}

public void LockPositionY()
{
        //Debug.Log ("Lock position Y");
        gObjTmp.transform.position = new Vector3 (this.transform.position.x, LinePosY, this.transform.position.z);
        //Debug.Log (gObjTmp.transform.position);
    lockPosY = true;
    lockPosX = false;
    //Debug.Log (lockPosX);
    //Debug.Log (lockPosY);

}

public void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "LineL" && lockPosX == false) 
    {
        if (lockPosY == false) 
        {
            //Debug.Log ("Collision with lineL");
            LinePosX = col.transform.position.x;
            LockPositionX ();

        } 
    }
    if (col.gameObject.tag == "LineR" && lockPosX == false) 
    {
        if (lockPosY == false) 
        {
            //Debug.Log ("Collision with lineR");
            LinePosX = col.transform.position.x;
            LinePosX -= 8;
            LockPositionX ();

        }
    }
    if (col.gameObject.tag == "LineTop" && lockPosY == false) 
    {
        if (lockPosX == false) 
        {
            //Debug.Log ("Collision with lineTop");
            LinePosY = col.transform.position.y;
            LockPositionY ();
        } 
    }
}

}

Upvotes: 0

Views: 461

Answers (2)

P1CACHU
P1CACHU

Reputation: 1

Your code never calls some Unlock method. After first Lock you can't pass checks in the OnCollisionEnter method. Both of your lockPos variables must be false when the collision happens

In addition. I'd prefer such variant:

    public void LockPositionX()
    {
        gObjTmp.transform.position = new Vector3 ((LinePosX + 4), this.transform.position.y, this.transform.position.z);
        LockPosX = true;
    }

    public void LockPositionY()
    {
        gObjTmp.transform.position = new Vector3 (this.transform.position.x, (LinePosY - 4), this.transform.position.z);
        LockPosY = true;
    }

    public void OnCollisionEnter(Collision col)
    {
        UnlockPos ();   
        if (col.gameObject.tag == "LineL") 
        {
            LinePosX = col.transform.position.x;
            LockPositionX ();
        }
        if (col.gameObject.tag == "LineR") 
        {
            LinePosX = col.transform.position.x - 8;
            LockPositionX ();
        }
        if (col.gameObject.tag == "LineTop") 
        {       
            LinePosY = col.transform.position.y;
            LockPositionY ();
        }
        if (col.gameObject.tag == "LinesBot")
        {
            LinePosY = col.transform.position.y + 8;
            LockPositionY ();
        } 
    }

    public void UnlockPos()
    {
        gObjTmp.transform.position = new Vector3 (this.transform.position.x, this.transform.transform.position.y, this.transform.position.z);
        LockPosX = false;
        LockPosY = false;
    }

Upvotes: 0

Kia Jun
Kia Jun

Reputation: 87

Edited the code,maybe something like this? @P1CACHU

public void LockPositionX()
{
    if(LockPosX == true)
    {
        Debug.Log ("Lock positionX");
        gObjTmp.transform.position = new Vector3 ((LinePosX + 4), this.transform.position.y, this.transform.position.z);
        //Debug.Log (gObjTmp.transform.position);
    }
}

public void LockPositionY()
{
    if (LockPosY == true) 
    {
        Debug.Log ("Lock position Y");
        gObjTmp.transform.position = new Vector3 (this.transform.position.x, (LinePosY - 4), this.transform.position.z);
        Debug.Log (gObjTmp.transform.position);
    }
}

public void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "LineL" && LockPosX == false && LockPosY == false) 
        {
            Debug.Log ("Collision with lineL");
            LinePosX = col.transform.position.x;
            LockPosX = true;
            LockPosY = false;
            LockPositionX ();
    }
    if (col.gameObject.tag == "LineR" && LockPosX == false && LockPosY == false) 
    {
            Debug.Log ("Collision with lineR");
            LinePosX = col.transform.position.x;
            LinePosX -= 8;
            LockPosX = true;
            LockPosY = false;
        LockPositionX ();
    }
    if (col.gameObject.tag == "LineTop" && LockPosY == false && LockPosX == false) 
    {       
            Debug.Log ("Collision with lineTop");
            LinePosY = col.transform.position.y;
            LockPosY = true;
            LockPosX = false;
            LockPositionY ();
    }

    if (col.gameObject.tag == "LinesBot" && LockPosY == false && LockPosX == false) {
        Debug.Log ("Collision with lineBot");
        LinePosY = col.transform.position.y;
        LinePosY += 8;
        LockPosY = true;
        LockPosX = false;
        LockPositionY ();
    } 
    else 
    {
        UnlockPos ();   
    }

}

public void UnlockPos()
{
    gObjTmp.transform.position = new Vector3 (this.transform.position.x, this.transform.transform.position.y, this.transform.position.z);
    LockPosX = false;
    LockPosY = false;
    Debug.Log ("unlocked pos");
}
}

Upvotes: 0

Related Questions