Script Conversion once more.

Hopefully this is the last time that I ask about getting help with a script conversion for a week or two :D.

I am converting the Aim Down Sights Script on Unify into C#. (Well Parts of it)

Here is the Java:

var gun : Transform;
static var nextPos = 0.1;
static var nextField = 60.0;
static var nextPos2 = -1.55;
var dampVelocity = 0.4;
var dampVelocity2 = 0.4;
var dampVelocity3 = 0.4;

function Update () {
   var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3);
   var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3);
   var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

   gun.transform.localPosition.x = newPos;
   gun.transform.localPosition.y = newPos2;
   Camera.main.fieldOfView = newField;

   if (Input.GetButton("Fire2")) {
       //adjust viewpoint and gun position
       nextField = 55.0;
       nextPos = -0.13;
       nextPos2 = -1.45;

   } else {
       //adjust viewpoint and gun position
       nextField = 60.0;
       nextPos = 0.1;
       nextPos2 = -1.55;

   }
}

and this is my c# thus far:

using UnityEngine;
using System.Collections;

public class ADS : MonoBehaviour {
public Transform gun;
private float nextPos = 0.1f;
private float nextField = 60.0f;
private float nextPos2 = -1.55f;
private float dampVelocity = 0.4f;
private float dampVelocity2 = 0.4f;
private float dampVelocity3 = 0.4f;

void Update (){
   float newPos= Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, ref dampVelocity, .3f);
   float newField= Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, ref dampVelocity2, .3f);
   float newPos2= Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, ref dampVelocity3, .3f);

   gun.transform.localPosition.x = newPos;
   gun.transform.localPosition.y = newPos2;
   Camera.main.fieldOfView = newField;

   if (Input.GetButton("Fire2")) {
       //adjust viewpoint and gun position
       nextField = 55.0f;
       nextPos = -0.13f;
       nextPos2 = -1.45f;

   } else {
       //adjust viewpoint and gun position
       nextField = 60.0f;
       nextPos = 0.1f;
       nextPos2 = -1.55f;

   }
}
}

I am getting the error "ADS.cs(18,18): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable"

Unsure on what to do.

Cheers, Aaron Lewis

The problem you are asking about is that in C#, you have to assign a complete Vector3 to transform.localPosition. You also have some weirdness going on with repetitive variable names and code, and you're assigning values outside of functions, which is not recommended. If you really are going to hard-code these (which I doubt, so get rid of [HideInInspector] and use the Inspector), then at least serialize the values so they have more of a chance of working.

[HideInInspector] [SerializeField] Vector3 nextPos = new Vector3(.1F, -1.55F);
[HideInInspector] [SerializeField] float[] dampVelocity13 = {.4F, .4F};

for (int i = 0; i < 2; ++i) nextPos *= Mathf.SmoothDamp(*
 <em>gun.transform.localPosition<em>, nextPos_, ref dampVelocity13*, .3F);*_</em></em>
<em><em>_*gun.transform.localPosition = nextPos;*_</em></em>
<em><em>_*```*_</em></em>