Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Error When Changing Rigidbody2D Constraints Through C#

Discussion in 'Scripting' started by nathukadevyt, Apr 7, 2021.

  1. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    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<Rigidbody2D>();
    }

    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 :)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    ClaspedNex and nathukadevyt like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Some specific help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyy':"

    http://plbm.com/?p=263
     
    nathukadevyt likes this.
  4. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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!
     
    nathukadevyt likes this.
  6. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    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!)
     
    Kurt-Dekker likes this.
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    The link I shared includes all of the possible values of that enum.
     
    nathukadevyt likes this.
  8. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    Okay Now I feel Plain Up Stupid

    Thanks Kurt And Praetor For Your Help!