Error When Changing Rigidbody2D Constraints Through C#

I’m Making A Teleporter Function For My 2D Game. I’m not going off any tutorial, just winging it off my current experience and knowledge of coding. Everything was going well, but I encountered An Error When Teleporting Between Transform Positions. After Teleporting, My Player GameObject would be moved over by a fraction, and clip through the wall.

Immediately, The First Thing I thought of to fix it was to freeze the rigidbody constraints through An Interactions Script And Use A Time Delay To Stop The Player From Clipping Through The Wall. Although, I got this error which is preventing me from moving onto the next feature.


“error CS0266: Cannot implicitly convert type ‘UnityEngine.RigidbodyConstraints’ to ‘UnityEngine.RigidbodyConstraints2D’. An explicit conversion exists (are you missing a cast?)”

Here Is My Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DirectInteraction : MonoBehaviour
{
[Header(“Variables”)]

[SerializeField]
public float TeleportWaitTime;

Rigidbody2D playerrig;

void Start()
{
playerrig = GetComponent();
}

IEnumerator OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag(“Teleporter”))
{
playerrig.constraints = RigidbodyConstraints.FreezePositionX;

yield return new WaitForSeconds(TeleportWaitTime);

playerrig.constraints = RigidbodyConstraints.None;
playerrig.constraints = RigidbodyConstraints.FreezeRotationZ;
}
}
}

Anyone Know A Solution? This Has Been Really Bugging Me. Any Help Would Be Appreciated :slight_smile:

You need to use RigidbodyConstraaints2D for a Rigidbody2D

2 Likes

Praetor has it identified above, precisely as what the error says.

Generally, here is how to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

Some specific help to fix “Cannot implicitly convert type ‘Xxxxx’ into ‘Yyyy’:”

http://plbm.com/?p=263

1 Like

If what you say is the answer, I feel really stupid for not trying that beforehand

No need to feel stupid… it’s a biiiiiiiig API with a lot of similar-sounding bits.

It’s just good to get faster at spotting such a “almost but not quite!” situation!

1 Like

Well, I must have tried doing that beforehand, since Im getting a new error:

“error CS0117: ‘RigidbodyConstraints2D’ does not contain a definition for ‘FreezeRotationZ’”

Anyways, thanks for your help! I will do some research on how to fix this (Im very braindead when it comes to rigidbody physics!)

1 Like

The link I shared includes all of the possible values of that enum.

1 Like

Okay Now I feel Plain Up Stupid

Thanks Kurt And Praetor For Your Help!