- Home /
some piston "ai"?
so i want to make a piston and i am stuck with oncollisionenter2d, i want to make it trigger something when it touches a tag called terrain.
bool NotStuck = true;
void Update()
{
if (NotStuck == true)
transform.Translate(Vector3.down * Time.deltaTime);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "terrain")
{
Debug.Log("OH SHIT");
NotStuck = false;
}
}
so all i want is make it so it does not need rigidbody2d (because it will be moving down ) while it stops when touching ground.
Answer by ahsen35813 · Apr 28, 2020 at 01:58 PM
First of all, I would advise that you use collision.gameObject.CompareTag("terrain")
because it preforms better.
Now to address your main problem, if you want the script to register a collision, at least one of the two objects have to have a rigidbody. However, you can set the rigidbody of the piston to be Kinematic. You can do this in the editor by clicking the isKinematic checkbox, or you can do it via script like this. Kinematic just prevents it from being affected by any forces such as gravity or collsions with other objects.
void Start()
{
gameObject.GetComponent<Rigidbody2D>.isKinematic = true;
}
If you do this, it should acheive the desired affect and still work. I hope this helps!