简体   繁体   English

无法将类型“float”隐式转换为“UnityEngine.Quaternion”

[英]Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion'

I am working on a 2D game, but I need to be able to shoot in the direction of the mouse, so I am trying to set the rotation of an empty to point towards the mouse so that the projectile can shoot from the empty because I don't want the character to rotate, but I get an error on the last line "Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion'" How do I fix this?我正在开发一个 2D 游戏,但我需要能够朝鼠标的方向射击,所以我试图将空的旋转设置为指向鼠标,以便弹丸可以从空射击,因为我不希望角色旋转,但我在最后一行出现错误“无法将类型'float'隐式转换为'UnityEngine.Quaternion'”我该如何解决这个问题? I can't use a Rigidbody 2D because the empty is parented and inside of a player with a RidgidBody2D and collider.我不能使用 Rigidbody 2D 因为空是父级的,并且在带有 RidgidBody2D 和对撞机的玩家内部。 If there is a better way to do this (which there probably is), let me know.如果有更好的方法来做到这一点(可能有),请告诉我。 Sorry if this is a dumb question, I am new to Unity and C#.抱歉,如果这是一个愚蠢的问题,我是 Unity 和 C# 的新手。

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

public class Point_and_Shoot : MonoBehaviour
{
    public Camera cam;
    public Rigidbody2D rb; //Ridgidbody of player
    public Transform rb2; //Transform of empty (empty is at same position as player but it's parented so I have to take position from player because the position is relative to the parent)
    Vector2 mousePos;

    void Update()
    {
        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }
    void FixedUpdate()
    {
        Vector2 lookDir = mousePos - rb.position;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
        rb2.rotation = angle; //<-- The error is here
    }
}

something like this would work像这样的东西会起作用

    float angle = Mathf.Atan2(1, 2) * Mathf.Rad2Deg - 90f;
    rb2.rotation = new Quaternion(angle, 0, 0, 0);

Instead of setting the rotation to a single value, you can use the constructor Quaternion .您可以使用构造函数Quaternion ,而不是将旋转设置为单个值。 You enter four values.您输入四个值。 The first three being the x, y, and z.前三个是 x、y 和 z。 The fourth isn't needed in this case.在这种情况下不需要第四个。 Only type Quaternion works here .只有四元数类型在这里有效 You can't use Vector3, or Vector2.您不能使用 Vector3 或 Vector2。 You can move the angle variable to y or z places because you might not want to rotate the way it was written above.您可以将角度变量移动到 y 或 z 位置,因为您可能不想像上面写的那样旋转它。 A link 一条链接

read up on the type system阅读类型系统

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/

this is a good explination这是一个很好的解释

Quaternion represents the rotation for the whole object, not just an angle.四元数代表整个 object 的旋转,而不仅仅是一个角度。 Try to set eulerAngles directly, since it accepts angles as floats (Vector3).尝试直接设置 eulerAngles,因为它接受角度为浮点数 (Vector3)。 If you want to have a 90° rotation around the Z axis, it would look something like this:如果你想绕 Z 轴旋转 90°,它看起来像这样:

eulerAngles = new Vector3(0, 0, 90); // Create/set euler angles in degrees.

quaternion.eulerAngles = eulerAngles; // Apply set euler angles to our quaternion

transform.rotation = quaternion; // Apply quaternion to our game object/transform

If you want to create some kind of continuous (rotation) motion, you should keep Time.Delta time in mind as a factor.如果要创建某种连续(旋转)运动,则应牢记 Time.Delta 时间作为一个因素。

Your "angle" variable is of a float data type.您的“角度”变量是浮点数据类型。 This cannot be assigned to Quaternion, as Quaternion calls for 4 values (float is just assigning 1).这不能分配给四元数,因为四元数需要 4 个值(浮点数只是分配 1)。 Alternatively, you could do this:或者,您可以这样做:

public Quaternion rb2.rotation;

and then assign the angle into quaternion in the form (angle, value, value, value)然后将角度以 (angle, value, value, value) 的形式赋值给四元数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“无法将转换类型‘float’隐式转换为‘UnityEngine.Quaternion’”? - How do I fix " Cannot implicitly convert convert type 'float' to 'UnityEngine.Quaternion' "? 参数 1:无法从 'float' 转换为 'UnityEngine.Quaternion' - Argument 1: cannot convert from 'float' to 'UnityEngine.Quaternion' 我如何解决“错误 CS0029 无法将类型 &#39;UnityEngine.Quaternion&#39; 隐式转换为 &#39;UnityEngine.Vector3” - How do i fix "error CS0029 Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3" 无法将类型“unityengine.vector3”隐式转换为“float” - cannot implicitly convert type 'unityengine.vector3' to 'float' CS1503:参数 3:无法从“double”转换为“UnityEngine.Quaternion”帮助! 统一二维 c# - CS1503: Argument 3: cannot convert from 'double' to 'UnityEngine.Quaternion' HELP! unity 2D c# 无法隐式转换类型'UnityEngine.Vector2?'到'UnityEngine.Vector2' - Cannot implicitly convert type 'UnityEngine.Vector2?' to 'UnityEngine.Vector2' 无法将类型“UnityEngine.AudioClip”隐式转换为“UnityEngine.AudioSource” - Cannot implicitly convert type 'UnityEngine.AudioClip' to 'UnityEngine.AudioSource' unity 5.3.0f4错误CS0029;无法将类型&#39;UnityEngine.Vector3&#39;隐式转换为&#39;float&#39; - unity 5.3.0f4 error CS0029;cannot implicitly convert type 'UnityEngine.Vector3' to 'float' 我如何在统一中修复此错误:无法将类型浮点隐式转换为 UnityEngine.transform - How do I fix this error on unity: Cannot implicitly convert type float to UnityEngine.transform 无法将类型 &#39;(float xpos, int, int)&#39; 隐式转换为 &#39;UnityEngine.Vector3&#39; - Cannot implicitly convert type '(float xpos, int, int)' to 'UnityEngine.Vector3'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM