Not sure how to fix this. Here's what it says in unity.> Assets/MinionController.cs(35,17): error CS0128: A local variable named `velocity' is already defined in this scope
When I hover over the 'velocity' on line 35, it says this: "a local or parameter named 'velocity' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Networking;
struct Snapshot
{
readonly Vector3 positionOnArrival;
readonly Vector3 velocityOnArrival;
readonly Vector3 reportedPosition;
readonly Vector3 reportedVelocity;
// Using floats for time works well for games that can go for hours.
// Switch to doubles or other formats for games expected to run persistently.
readonly float arrivalTime;
readonly float blendWindow;
public Snapshot(Vector3 currentPosition, Vector3 currentVelocity,
Vector3 reportedPosition, Vector3 reportedVelocity,
float blendWindow)
{
arrivalTime = Time.time;
positionOnArrival = currentPosition;
velocityOnArrival = currentVelocity;
this.reportedPosition = reportedPosition;
this.reportedVelocity = reportedVelocity;
this.blendWindow = blendWindow;
}
Vector3 EstimatePosition(float time, out Vector3 velocity)
{
float dT = time - arrivalTime;
float blend = Mathf.Clamp01(dT / blendWindow);
Vector3 velocity = Vector3.Lerp(velocityOnArrival, reportedVelocity, blend);
Vector3 position = Vector3.Lerp(
positionOnArrival + velocity * dT,
reportedPosition + reportedVelocity * dT,
blend);
return position;
}
}
public class MinionController : NetworkBehaviour {
public Camera cam;
public NavMeshAgent agent;
//Vector3 velocity;
Vector3 bestGuessPosition;
float ourLatency;
float latencySmoothingFactor = 10;
private void Start()
{
Snapshot currentSnapshot;
}
// Update is called once per frame
void Update()
{
if (hasAuthority == false)
{
bestGuessPosition = bestGuessPosition + (velocity * Time.deltaTime);
transform.position = currentSnapshot.EstimatePosition(Time.time, out velocity);
return;
}
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
agent.SetDestination(hit.point);
}
}
}
[Command]
void CmdUpdateVelocity(Vector3 v, Vector3 p)
{
transform.position = p;
velocity = v;
RpcUpdateVelocity(velocity, transform.position);
}
[ClientRpc]
void RpcUpdateVelocity(Vector3 v, Vector3 p)
{
if (hasAuthority)
return;
currentSnapshot = new Snapshot(
transform.position, velocity,
p, v,
updatePacketPeriod // Expected time between new position/velocity reports.
);
}
}
↧