Ошибка в юнити:error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion'
Assets\Scripts\RayPlayer.cs(33,59): error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion'
вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayPlayer : MonoBehaviour
{
public GameObject theDest;
public bool pickUp;
private void Update(){
if(pickUp){
if(Input.GetKeyDown(KeyCode.Q)){
theDest.transform.GetChild(0).GetComponent<Rigidbody>().isKinematic = false;
theDest.transform.GetChild(0).GetComponent<Rigidbody>().useGravity = true;
theDest.transform.GetChild(0).GetComponent<BoxCollider>().enabled = true;
theDest.transform.GetChild(0).parent = null;
pickUp = false;
}
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
if(Physics.Raycast(ray, out hit, 5f)){
if(hit.collider.tag == "flashlight") {
if (pickUp == false){
if(Input.GetKeyDown(KeyCode.E)){
hit.collider.transform.parent = theDest.transform;
hit.collider.GetComponent<Rigidbody>().isKinematic = true;
hit.collider.GetComponent<Rigidbody>().useGravity = false;
hit.collider.GetComponent<BoxCollider>().enabled = false;
hit.collider.transform.position = theDest.GetComponent<Transform>().position;
hit.collider.transform.rotation = theDest.GetComponent<Transform>().rotation.x;
hit.collider.transform.rotation = theDest.GetComponent<Transform>().rotation.z;
pickUp = true;
}
}
}
}
}
}
Ошибка возникает из-за того, что вы пытаетесь присвоить значение типа float переменной типа UnityEngine.Quaternion. В строке 33 вашего кода вы пытаетесь присвоить значение theDest.GetComponent<Transform>().rotation.x переменной hit.collider.transform.rotation, которая является типом UnityEngine.Quaternion. Однако, theDest.GetComponent<Transform>().rotation.x является значением типа float. Чтобы исправить эту ошибку, вам необходимо присвоить значение типа UnityEngine.Quaternion переменной hit.collider.transform.rotation. Вы можете использовать метод Quaternion.Euler для создания кватерниона из углов Эйлера. Например, вы можете заменить строку 33 на следующую строку кода: hit.collider.transform.rotation = Quaternion.Euler(theDest.GetComponent<Transform>().rotation.eulerAngles); Это должно исправить ошибку.