I have many functions working basically the same way, just using different variables. A brief example:
[SyncVar] private Vector3 syncedPlayerPosition;
[SyncVar] private Vector3 syncedHandPosition;
private Transform playerTransform;
private Transform handTransform;
private float lerpPlayerPositionRate;
private float lerpHandPositionRate;
void SetPlayerPosition(){
if(playerTransform.position != syncedPlayerPosition){
playerTransform.position = Vector3.Lerp(playerTransform.position, syncedPlayerPosition, Time.deltaTime * lerpPlayerPositionRate);
}
}
void SetHandPosition(){
if(handTransform.position != syncedHandPosition){
handTransform.position = Vector3.Lerp(handTransform.position, syncedHandPosition, Time.deltaTime * lerpHandPositionRate);
}
}
As you can see, both functions do basically the same thing that they just use different variables.
Now I'm looking for something like this (assuming it Dictionary
will work like that)
[SyncVar] private Vector3 syncedPlayerPosition;
[SyncVar] private Vector3 syncedHandPosition;
private Transform playerTransform;
private Transform handTransform;
private float lerpPlayerPositionRate;
private float lerpHandPositionRate;
private Dictionary<Transform,Vector3> positionDict = new Dictionary<Transform,Vector3>();
private Dictionary<Transform,float> lerpRateDict = new Dictionary<Transform,float>();
private void Start(){
positionDict.Add(playerTransform, syncedPlayerPosition);
positionDict.Add(handTransform, syncedHandPosition);
lerpRateDict.Add(playerTransform, lerpPlayerPositionRate);
lerpRateDict.Add(handTransform, lerpHandPositionRate);
}
void SetPosition(Transform transform){
if(transform.position != positionDict[transform]){
transform.position = Vector3.Lerp(transform.position, positionDict[transform], Time.deltaTime * lerpRateDict[transform]);
}
lerpRateDict[transform] += 1;
positionDict[transform] = Vector3.zero();
}
I actually tried it this way, but realized that the values in are Dictionary
not updated, but keep everything where they were when called Start()
. Therefore, it seems to me that they are not added to Dictionary
on Reference
, but on Value
.
Is it possible to configure it so that the dictionaries do not actually store values, but rather links to existing variables? How can I archive something like that?
, . , , .
, : hte Vector3
float
Transform
s.