Unity3D - Ошибка, говорящая, что неожиданный символ "расширяется" - PullRequest
0 голосов
/ 05 июля 2018

Unity3D 2018.1.6

Сценарий изначально был javascript, но преобразован в csharp. Теперь у меня есть ошибка для класса, говорящая, что неожиданно я использовал `extends '

Error

Неожиданный символ "расширяется"

Я попробовал 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;
}
}

У меня есть второй сценарий с той же ошибкой, но при решении одного сценария я уверен, что смогу решить другой.

Спасибо за помощь!

Ответы [ 2 ]

0 голосов
/ 05 июля 2018

Эквивалент 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 { }

Для других сценариев в этом проекте , которые не используют ключевое слово extend, они просто получаются из MonoBehaviour при преобразовании в C #.

Например:

class OtherScripts : MonoBehaviour { }
0 голосов
/ 05 июля 2018

Расширения - это ключевое слово Java. Не C # и не JavaScript.

В C # вы используете двоеточие, чтобы сказать extends, в Java вы используете слово.

Вы не можете использовать JavaScript или Java при написании кода на C #.

public class OrbitState extends Object : MonoBehaviour {

Должно быть

public class OrbitState : MonoBehaviour {
...