Reputation: 87
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);
}
}
Upvotes: 0
Views: 9668
Reputation: 125275
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);
}
}
Upvotes: 1