Изменение переменной на свойство создает ошибкуC#

Место общения программистов C#
Ответить
Anonymous
 Изменение переменной на свойство создает ошибку

Сообщение Anonymous »

У меня есть проект Unity. Защищенная переменная вектора2 (скорость) в абстрактном классе (MovementAgent), используемой агентом, который наследует от этого класса. Ошибки. < /p>
public abstract class MovementAgent : MonoBehaviour where T : MovementAgentSO
{
protected Vector2 _velocity;
protected Vector2 velocity { get => _velocity; set => _velocity = value; }
public Vector2 GetVelocity() => velocity;

[NonSerialized] protected T Settings;

///
/// Initializes the MovementAgent to use the settings ScriptableObject provided as a parameter.
///
/// Settings ScriptableObject reference
public void Initialize(T settings)
{
Settings = settings;
}

///
/// Processes the movement of the agent and keeps its rotation aligned with its velocity.
///
protected void ProcessMovement()
{
if (velocity.magnitude > Settings.maxSpeed)
{
velocity.Normalize();
velocity *= Settings.maxSpeed;
}

transform.rotation = Quaternion.Euler(0, 0, (float)(Math.Atan2(velocity.y, velocity.x) / Math.PI * 180));

transform.position = (Vector2)transform.position + velocity * Time.deltaTime;
}

public Vector2 Seek(Vector2 target)
{
Vector2 desiredVelocity = target - (Vector2)transform.position;
desiredVelocity.Normalize();
desiredVelocity *= Settings.maxSpeed;

Vector2 turnVector = desiredVelocity - velocity;
if (turnVector.magnitude > Settings.maxTurnSpeed)
{
turnVector.Normalize();
turnVector *= Settings.maxTurnSpeed;
}

return turnVector;
}

public Vector2 Arrive(Vector2 target, float radius)
{
Vector2 difference = target - (Vector2)transform.position;
float distance = difference.magnitude;

if (distance > radius) return Seek(target);

Vector2 desiredVelocity;
if (radius == 0)
desiredVelocity = difference.normalized * Settings.maxSpeed;
else
desiredVelocity = difference.normalized * (Settings.maxSpeed * distance / radius);

Vector2 steering = desiredVelocity - velocity;
steering = Vector3.ClampMagnitude(steering, Settings.maxTurnSpeed);
return steering;
}

public Vector2 Flee(Vector2 target)
{
return -Seek(target);
}
public Vector2 Pursue(Vector2 target, Vector2 targetSpeed, float predictionTime)
{
return Seek(target + targetSpeed * predictionTime);
}
public Vector2 Evade(Vector2 target, Vector2 targetSpeed, float predictionTime)
{
return -Pursue(target, targetSpeed, predictionTime);
}

public void AddVelocity(Vector2 velocityToAdd)
{
velocity += velocityToAdd;
}

public float GetMaxSpeed()
{
return Settings.maxSpeed;
}
}
< /code>
Если я шагаю по кадру за кадром на Unity, я вижу, как пара пустот внезапно начинает двигаться с большими приращениями, выходя за пределы диапазонов поплавок всего за пару кадров. Заранее спасибо!>

Подробнее здесь: https://stackoverflow.com/questions/797 ... s-an-error
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»