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

时间:2018-07-05 15:33:27

标签: javascript c# unity3d inheritance extends

Unity3D 2018.1.6

该脚本最初是javascript,但已将其转换为csharp。现在,我在该类中遇到一个错误,说我用`extends'了

错误

  

意外的符号“扩展”

例如,我尝试了两种使用extends作为javascript的方式

第一方式1)

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

第二种方式)

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

我有另一个脚本,但有相同的错误,但是在解决一个脚本时,我确定可以解决另一个脚本。

谢谢您的帮助!

2 个答案:

答案 0 :(得分:4)

在C#中,等效于extends关键字的是:

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 { }

答案 1 :(得分:1)

扩展是Java关键字。不是C#也不是JavaScript。

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

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

public class OrbitState extends Object : MonoBehaviour {

应该是

public class OrbitState : MonoBehaviour {