Unity3D - 错误说意外符号“扩展”

Unity3D - Error saying Unexpected symbol `extends'

Unity3D 2018.1.6

该脚本最初是 javascript 但将其转换为 csharp。现在我有一个关于 class 的错误,说我使用 `extends'

出乎意料

错误

Unexpected symbol `extends'

我尝试了 2 种使用 extends 的方法,因为 javascript 有它,例如

第一种方法 1)

public class ScriptOne : MonoBehaviour {
    //
}
class ScriptOne extends Object {
    //
}

第二种方式 2)

public class ScriptTwo extends Object : MonoBehaviour {
    //
}

两种方式都给了我同样的错误。

这是从 .js 转换为 .cs 的脚本之一

using UnityEngine;
using System.Collections;

public class OrbitState extends Object : MonoBehaviour {


//================================//
//===   Orbit State datatype   ===//
//================================//

/*
 The OrbitState is the initial state of the orbiter at a particular point along the ellipse
 The state contains all of the information necessary to apply a force to get the orbiter moving along the ellipse
*/

Vector3 position; // local position relative to the object we're orbiting around
Vector3 normal;
Vector3 tangent;
Vector3 velocity;
private Orbiter orbiter;
private OrbitalEllipse ellipse; 

//==== Instance Methods ====//

// Constructor
void  OrbitState ( float angle ,   Orbiter orbiter ,   OrbitalEllipse ellipse  )
{
    Update(angle, orbiter, ellipse);
}

// Update the state of the orbiter when its position along the ellipse changes
// Note: Make sure the ellipse is up to date before updating the orbit state
void  Update ( float orbiterAngle ,   Orbiter orbiter ,   OrbitalEllipse ellipse  )
{
    this.orbiter = orbiter;
    this.ellipse = ellipse;
    this.normal = CalcNormal(orbiterAngle);
    this.tangent = CalcTangent(normal);
    this.position = ellipse.GetPosition(orbiterAngle, orbiter.orbitAround.position);
    this.velocity = CalcVelocity(orbiter.orbitSpeed * orbiter.GravityConstant(), position, orbiter.orbitAround.position);
}


//==== Private Methods ====//

// Returns the normal on the ellipse at the given angle
// Assumes a vertical semi-major axis, and a rotation of 0 at the top of the ellipse, going clockwise
private Vector3 CalcNormal ( float rotationAngle  )
{
    // Part 1: Find the normal for the orbiter at its starting angle
    // Rotate an upward vector by the given starting angle around the ellipse. Gives us the normal for a circle.
    Vector3 localNormal = Quaternion.AngleAxis(rotationAngle, Vector3.forward*-1) * Vector3.up;
    // Sqash the normal into the shape of the ellipse
    localNormal.x *= ellipse.semiMajorAxis/ellipse.semiMinorAxis;

    // Part 2: Find the global rotation of the ellipse
    float ellipseAngle = Vector3.Angle(Vector3.up, ellipse.difference);
    if (ellipse.difference.x < 0)
        ellipseAngle = 360-ellipseAngle; // Full 360 degrees, rather than doubling back after 180 degrees

    // Part 3: Rotate our normal to match the rotation of the ellipse
    Vector3 globalNormal = Quaternion.AngleAxis(ellipseAngle, Vector3.forward*-1) * localNormal;
    return globalNormal.normalized;
}

private Vector3 CalcTangent ( Vector3 normal  )
{
    float angle = 90;
    int direction = orbiter.counterclockwise ? -1 : 1;
    FIXME_VAR_TYPE tangent= Quaternion.AngleAxis(angle*direction, Vector3.forward*-1) * normal;
    return tangent;
}

private Vector3 CalcVelocity ( float gravity ,   Vector3 orbiterPos ,   Vector3 orbitAroundPos  )
{
    // Vis Viva equation
    float speed = Mathf.Sqrt( gravity * (2/Vector3.Distance(orbiterPos, orbitAroundPos) - 1/ellipse.semiMajorAxis ) );
    Vector3 velocityVec = tangent * speed;
    return velocityVec;
}
}

我有第二个脚本有同样的错误,但在解决一个脚本时我确信我可以解决另一个。

感谢您的帮助!

Extends 是一个 Java 关键字。不是 C# 也不是 JavaScript.

在 C# 中,您使用冒号表示扩展,在 Java 中,您使用单词。

您不能在编写 C# 代码时使用 JavaScript 或 Java。

public class OrbitState extends Object : MonoBehaviour {

应该是

public class OrbitState : MonoBehaviour {

extends 关键字等同于 C# 中的 :

UnityScript:

class OrbitState extends Object { //your code}

C#:

class OrbitState : UnityEngine.Object { //your code}`

请注意,我添加了 UnityEngine 命名空间,因为有 System.Object。那是另一种。


UnityScript:

class OrbitalEllipse extends Object {}

C#:

class OrbitalEllipse : UnityEngine.Object { }

对于 project 中未使用 extend 关键字的其他脚本,它们在转换为 C# 时仅派生自 MonoBehaviour

例如:

class OtherScripts : MonoBehaviour { }