Hunain Usman
Hunain Usman

Reputation: 2228

Cannot Access C# class in Javascript file in Unity

I am fairly new in Unity and cannot seem to estimate the cause for this bug, apparently i have maintained a class of static constants in my project in which i have various bool checks, now i have used smoothFollow Script in my project as well, but this is in JS.

The problem is i want to use a check in my C# Constants class in the JS smooth follow file, like

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

but i keep getting a bunch of syntax errors like unexpected token and expecting something

Upvotes: 0

Views: 86

Answers (2)

Hamza Hasan
Hamza Hasan

Reputation: 1398

Here is the C# version of smooth follow for you :)

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);
        }
    }
}

Upvotes: 0

Dương Minh Khoa
Dương Minh Khoa

Reputation: 186

C# and JS can't see each other at compile time (different compilers are used for each language). But you can access C# by putting C# script to Stand Asset folder .

You can read more at http://forum.unity3d.com/threads/accessing-c-variable-from-javascript.117264/

Upvotes: 2

Related Questions