IT Learning

実践形式でITのお勉強

Unity

【Unity】 error CS0029: Cannot implicitly convert type ‘UnityEngine.Quaternion’ to ‘UnityEngine.Vector3’が出た時の対処法

投稿日:2020年10月21日 更新日:

概要

UnityのC#開発においてtransformオブジェクトをスクリプトで触っていたら、以下のエラーが発生したため、その対処方法についてメモする。

error CS0029: Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'

環境

Windows10
Unity 2020.1.f1

エラーが発生したコード

public class BulletCon : MonoBehaviour
{
    Vector3 LocalPos;
    Vector3 LocalRot;

    // Start is called before the first frame update
    void Start()
    {
        LocalPos = this.transform.localPosition;
        LocalRot = this.transform.localRotation;
    }
}

原因

transform.localRotationはVector3型に代入できないのにVector3型に代入していた。localRotationでなくtransform.rotationの場合も同様。

対処法

Vector3型ではなくQuaternion型で変数を宣言してlocalRotationを代入した。

public class BulletCon : MonoBehaviour
{
    Vector3 LocalPos;
    Quaternion LocalRot;

    // Start is called before the first frame update
    void Start()
    {
        LocalPos = this.transform.localPosition;
        LocalRot = this.transform.localRotation;
    }
}

以上

Unityを一から学ぶのにおすすめの本はコチラ

Related

-Unity

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です