如何修复“错误CS0029无法将类型'UnityEngine.Quaternion'隐式转换为'UnityEngine.Vector3”

时间:2019-04-28 12:00:33

标签: c# unity3d

我正在编写一部分代码来旋转播放器并将其添加到另一个对象中。它说“错误CS0029无法将类型'UnityEngine.Quaternion'隐式转换为'UnityEngine.Vector3”

我尝试在Internet上查找,但是我认为他们的解决方案不能与我的代码一起使用。

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

public class BulletCreator : MonoBehaviour {

    public Vector3 MyGameObjectPosition;
    public Vector3 MyGameObjectRotation;



    //drag prefab here in editor
    public Transform InstantiateMe;

    private void Start()
    {



    }

    // Update is called once per frame
    void Update()
    {
        MyGameObjectPosition = GameObject.Find("L96_Sniper_Rifle").transform.position;
        MyGameObjectRotation = GameObject.Find("L96_Sniper_Rifle").transform.rotation;

        if (Input.GetMouseButtonDown(0))
        {
            //you don't have to instantiate at the transform's positio nand rotation, swap these for what suits your needs
            var go = Instantiate(InstantiateMe, MyGameObjectPosition, transform.rotation);

        }
    }
}

应将对象的旋转值设置为与播放器相同的值。

3 个答案:

答案 0 :(得分:0)

您在哪一行收到错误?这是不言自明的。 Vector3(三维矢量)和四元数(四元数是旋转的数学表示)是两个不同的事物,不能互相分配。

如果要旋转的XYZ表示(类似于编辑器显示的内容),则称为“欧拉角”。您可以像这样将四元数转换为其欧拉角表示形式:

Quaternion q;
Vector3 v = q.eulerAngles;

您可以将包含欧拉角的vector3转换为相应的四元数,如下所示:

Vector3 v;
Quaternion q = Quaternion.euler(v);

答案 1 :(得分:0)

只需更改此:

def category_list(request, pk=None):
    properties = Properties.objects.filter(category__id=int(pk))
    context = {
        'properties' : properties,
        'categories' : Category.objects.all()
    return render(request,'category.html',context)

对此:

public Vector3 MyGameObjectRotation;

其余的都应该起作用。

答案 2 :(得分:0)

这应该是您的脚本。

    public Vector3 MyGameObjectPosition;
public Quaternion MyGameObjectRotation;



//drag prefab here in editor
public Transform InstantiateMe;

private void Start()
{



}

// Update is called once per frame
void Update()
{
    MyGameObjectPosition = GameObject.Find("L96_Sniper_Rifle").transform.position;
    MyGameObjectRotation = GameObject.Find("L96_Sniper_Rifle").transform.rotation;

    if (Input.GetMouseButtonDown(0))
    {
        //you don't have to instantiate at the transform's positio nand rotation, swap these for what suits your needs
        var go = Instantiate(InstantiateMe, MyGameObjectPosition, transform.rotation);

    }
}