无法访问 Unity 中 Javascript 文件中的 C# 类

本文关键字:文件 Javascript 访问 Unity | 更新日期: 2023-09-27 18:35:16

我在 Unity 中相当新手,似乎无法估计此错误的原因,显然我在我的项目中维护了一类静态常量,其中我有各种布尔检查,现在我也在我的项目中使用了 smoothFollow 脚本,但这是在 JS 中。

问题是我想在 JS 平滑跟随文件中的 C# 常量类中使用检查,例如

if(Constants.isWheelCameraActive){
    wantedHeight = 6.5;
} else {
    wantedHeight = target.position.y+3 +  height_offsetY ;
}

但是我不断收到一堆语法错误,例如意外的令牌并期待某些东西

无法访问 Unity 中 Javascript 文件中的 C# 类

C# 和 JS 在编译时看不到对方(每种语言使用不同的编译器)。但是您可以通过将 C# 脚本放入 Stand Asset 文件夹来访问 C#。

您可以在以下位置阅读更多信息http://forum.unity3d.com/threads/accessing-c-variable-from-javascript.117264/

这是 C# 版本的平滑跟随:)

using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
    public bool shouldRotate = false;
    // The target we are following
    public Transform target;
    // The distance in the x-z plane to the target
    public float distance = 10.0f;
    // the height we want the camera to be above the target
    public float height = 5.0f;
    // How much we
    public float heightDamping = 2.0f;
    public float rotationDamping = 3.0f;
    float wantedRotationAngle;
    float wantedHeight;
    float currentRotationAngle;
    float currentHeight;
    Quaternion currentRotation;
    void LateUpdate ()
    {
        if (target) {
            // Calculate the current rotation angles
            wantedRotationAngle = target.eulerAngles.y;
            wantedHeight = target.position.y + height;
            currentRotationAngle = transform.eulerAngles.y;
            currentHeight = transform.position.y;
            // Damp the rotation around the y-axis
            currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
            // Damp the height
            currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
            // Convert the angle into a rotation
            currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
            // Set the position of the camera on the x-z plane to:
            // distance meters behind the target
            transform.position = target.position;
            transform.position -= currentRotation * Vector3.forward * distance;
            // Set the height of the camera
            transform.position = new Vector3 (transform.position.x, currentHeight, transform.position.z);
            // Always look at the target
            if (shouldRotate)
                transform.LookAt (target);
        }
    }
}