【问题标题】:Cannot convert from bool to string [closed]无法从布尔转换为字符串[关闭]
【发布时间】:2018-01-08 10:09:18
【问题描述】:

我正在尝试创建一个相机工具,允许相机围绕玩家旋转,然后在不再按住箭头键后重置其位置。但是当我尝试

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

public class CompleteCameraController : MonoBehaviour {

public Transform Player;
public float rotationSpeed = 10f;
public Vector3 offset;

private void Update()
{
        if (Input.GetKey(KeyCode.LeftArrow)){
            transform.RotateAround(Player.transform.position, Vector3.down, Time.deltaTime * 25);
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.RotateAround(Player.transform.position, Vector3.up, Time.deltaTime * 25);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.RotateAround(Player.transform.position, Vector3.left, Time.deltaTime * 25);
        }
        if ((Player != null) && Input.GetKey(KeyCode.DownArrow))
        {
            transform.RotateAround(Player.transform.position, Vector3.right, Time.deltaTime * 25);
        }
        if (Input.GetKey(KeyCode.LeftArrow !=null))
        {
            transform.LookAt(Player.transform);
        }
    }
    void LateUpdate()
    {
        Vector3 desiredPosition = Player.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, rotationSpeed * Time.deltaTime);
        transform.position = smoothedPosition;
    }
}

【问题讨论】:

  • 在哪一行出现错误?
  • 你在哪里尝试将 bool 转换为字符串?
  • “但是当我尝试”然后...?
  • 我在第 28 行得到它

标签: c# unity3d


【解决方案1】:

你可能在这里搞错了:

if (Input.GetKey(KeyCode.LeftArrow !=null))

不清楚为什么你有 != null 部分,删除它,你应该没问题

【讨论】:

    【解决方案2】:

    问题就出来了

    if (Input.GetKey(KeyCode.LeftArrow !=null))
    

    您正在将 KeyCode.LeftArrownull 进行比较,这不起作用,因为它是一个枚举。我认为您可以安全地删除 !=null 部分,您会没事的。

    更新

    在您发表评论后,我认为您需要执行以下操作:

    if ( !Input.GetKey(KeyCode.LeftArrow))
    {
        transform.LookAt(Player.transform);
    }
    

    【讨论】:

    • 但如果它等于 null ,我想将它重置为 Player 对象。因此,如果它没有被按住,它将实习生重新聚焦到播放器
    • 我已经更新了我的答案
    • ty 我可以从这里解决剩下的问题:D
    • @Kingwebbie 如果这解决了您的问题,请考虑接受答案作为解决方案,以便问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2021-05-05
    • 2010-10-18
    • 2022-08-10
    • 2020-02-03
    • 2015-09-29
    • 2014-06-27
    相关资源
    最近更新 更多