【问题标题】:Unity3D - Error saying Unexpected symbol `extends'Unity3D - 错误提示意外符号“扩展”
【发布时间】:2018-07-05 15:33:26
【问题描述】:

Unity3D 2018.1.6

该脚本最初是 javascript,但将其转换为 csharp。现在我的班级有一个错误,说我使用了“扩展”是出乎意料的

错误

意外符号“扩展”

例如,我尝试了两种使用 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;
}
}

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

感谢您的帮助!

【问题讨论】:

标签: javascript c# unity3d inheritance extends


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    public class OrbitState extends Object : MonoBehaviour {
    

    应该是

    public class OrbitState : MonoBehaviour {
    

    【讨论】:

    • 所以我不用说: Object 了? : MonoBehavior够了吗?
    • 正确。您只能从 C# 中的一个类扩展。
    • 应该是class OrbitState : Object 而不是class OrbitState : MonoBehaviour
    猜你喜欢
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多